PHP class constants, static properties, non-static properties of the difference between the detailed

Source: Internet
Author: User
I. Class constants: Values that remain constant in a class are defined as constants

The constants of a class cannot use the access restriction modifier, which is public, inheritable, and can be overridden by a quilt class, and constants that access a class must use a double colon::, you can access it using the class name or an instance of the class, because it is a constant, so the name cannot be used to represent the variable's symbol $.

You can define a value that is always the same in a class as a constant. You do not need to use the $ symbol when defining and using constants.

The value of a constant must be a fixed value, not a variable, a class property, a result of a mathematical operation, or a function call.

You can also define constants in the interface (interface). See the interface section in the documentation for more examples.

From PHP 5.3.0, a variable can be used to dynamically invoke a class. However, the value of the variable cannot be a keyword (such as self,parent or static).

Syntax: const constant= ' constant ';

Class myclass{Const constant = ' constant ';//type internal call public function test () {  echo self::constant. ' <br> '; }}myclass::test ();//class external access echo myclass::constant. ' <br> '; Method One $obj = new Myclass ();  Method two echo $obj:: Constant. ' <br> ';

Two. Static property: Is a variable, can be constant syntax access, that is::, can not be instantiated also can access

Syntax: public static $my _static = ' similar constants ';

Class myclass{public static $static _val = ' static property ';//class internal access public function test () {  return self:: $static _val. ' <BR&G t; '; }}//class external access//echo myclass::test (); Echo Myclass:: $static _val;  Method One $obj = new Myclass ();    Method two echo $obj:: $static _val;echo $obj->test ();

Three. Non-static properties: A normal variable that can be accessed after instantiation, with the symbol

Syntax: public $my _val = ' normal variable '

Class myclass{public $normal _val = ' non-static property ';//class internal access public function test () {  return $this->normal_val;}} Class external Access $obj = new Myclass (); echo $obj->normal_val;echo $obj->test ();

PHP gets methods for constants, attributes, and method lists in a class

$r = new Reflectionclass ($this); Zend_debug::d UMP ($r->getconstants (), "Constants"); Zend_debug::d UMP ($r->getproperties (), "Properties"); Zend_debug::d UMP ($r->getmethods (), "Methods");
Related Article

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.