PHP Object-oriented? Static members

Source: Internet
Author: User
PHP Object-oriented? Static members

Static properties

Properties declared with the static keyword

The static property, logically, is the attribute defined above the class. guarantees a class that corresponds to a property.

Example:

Class Student

{

Public $stu _id;

Public $stu _name;

public static $stu _count = 0;

Public Function __constuct ($id, $name)

{

$this->stu_id = $id;

$this->stu_name = $name;

}
}

To access static properties:

Access by class: At the use of static accesses symbols (::)

Class:: Member

Example:

Class Student

{

Public $stu _id;

Public $stu _name;

public static $stu _count = 0;

Public Function __constuct ($id, $name)

{

$this->stu_id = $id;

$this->stu_name = $name;

Student:: $stu _count + +;

}
}

:: Access is called static access,-> for non-static access (object access)

When accessing a static property, if it is accessed within a class: You can use the Self current class

Example:

Class Student

{

Public $stu _id;

Public $stu _name;

public static $stu _count = 0;

Public Function __constuct ($id, $name)

{

$this->stu_id = $id;

$this->stu_name = $name;

Self:: $stu _count + +;

}
}

Note: The difference between $this and self

$this represents this object, and self indicates that the class

Static methods

The logical meaning of the static method is also the method of defining the re-class. Similarly, call form class::

Example:

Class Student

{

Public $stu _id;

Public $stu _name;

public static $stu _count = 0;

Public Function __constuct ($id, $name)

{

$this->stu_id = $id;

$this->stu_name = $name;

Self:: $stu _count + +;

}

public static function Saycount ()

{

echo "Run Static";

}
}

Student::saycount ();

The main difference between static and non-static methods is whether you can receive an object's execution environment.

Is whether the $this within the method can be assigned

Only when an object invokes a method can the object execution environment be passed into a method, $this is assigned a value.

Non-static properties cannot be used in static methods.

A static method that can handle only static data (static properties)

Example:

Class Student

{

Public $stu _id;

Public $stu _name;

public static $stu _count = 0;

Public Function __constuct ($id, $name)

{

$this->stu_id = $id;

$this->stu_name = $name;

Self:: $stu _count + +;

}

public static function Saycount ()

{

echo Self:: $stu _count;

}
}

Student::saycount ();

Special method access Issues in PHP:

Emphasis: Static methods should be called by the class, and non-static methods should be called by the object

Class C

{

Public Function F1 ()

{

Echo ' F1
’;

}

public static function F2 ()

{

Echo ' F2
’;

}

}

$o = new C;

$o->f1 ();

C::f2 ();

But:

1. Class access is available for both static and non-static methods: However, if a class statically invokes a non-static method, an error with a nonstandard syntax is reported.

2. The difference lies in the $this

You can use $this within a method only if you use the object to invoke a non-static method.

Class C

{

Public Function F1 ()

{

Echo ' F1
’;

}

public static function F2 ()

{

Echo ' F2
’;

}

}

C::F1 ();

C::f2 ();

Summary: The static and non-static methods of the class are only one copy of the class, but the runtime environment is different. You can use $this within a method only if you use the object to invoke a non-static method.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.