The fourth article in the object-oriented series of browser learning PHP -- keywords and object-oriented fourth article
* Directory [1] public [2] protected [3] private [4] final [5] static [6] const [7] this [8] self [9] parent
A notable feature of php's object-oriented implementation is the extensive use of keywords. This article will detail the keywords
Public
Public indicates public, which has the maximum access permission. It is defined as a public class member that can be accessed anywhere.
If the attribute is defined by var, it is regarded as public. If no keyword is set for the method, the method is public by default.
<?phpclass demo{ public $public = 1; function test($var){ echo "{$var}000"; }}$d1 = new demo;$d1->test($d1->public);//1000?>
Protected
Protected indicates protected. a protected class member can be accessed by itself, its subclass, and its parent class.
<?phpclass demo{ protected function fn(){ echo '111'; }}class demo1 extends demo{ function test(){ parent::fn(); } }$d1 = new demo1;$d1->test();//111?>
Private
Private indicates private. A class member defined as private can only be accessed by the class it defines
<?phpclass demo{ private $private = 1; function test(){ echo($this->private); }}$d1 = new demo;$d1->test();//1?>
Final
PHP5 adds the final keyword, which can only be used to modify classes and methods. The final keyword cannot be used to modify member attributes, because final is a constant, we define constants in PHP using the define () function and the const keyword, so we cannot use final to define Member attributes.
If the method in the parent class is declared as final, the subclass cannot overwrite the method. If a class is declared as final, it cannot be inherited.
<?phpclass BaseClass { public function test() { echo "BaseClass::test() called\n"; } final public function moreTesting() { echo "BaseClass::moreTesting() called\n"; }}class ChildClass extends BaseClass { public function moreTesting() { echo "ChildClass::moreTesting() called\n"; }}// Results in Fatal error: Cannot override final method BaseClass::moreTesting()?>
Static
The static keyword indicates the static meaning. It is used to modify the member attributes and member methods of a class (that is, static attributes and static methods)
The static attributes and static methods in the class can be accessed directly using the class name without instantiating (new ).
[Note] Static attributes cannot be accessed through an object that has been instantiated in the class, but static methods can
Because static methods can be called without passing through objects, the pseudo variable $ this is not available in static methods. Static attributes cannot be accessed by objects through the-> operator.
Calling a non-static method in static mode will result in an E_STRICT error.
Just like all other PHP static variables, static attributes can only be initialized as text or constants and cannot use expressions. Therefore, static attributes can be initialized as integers or arrays, but they cannot be initialized as another variable or function return value or point to an object.
<? Phpclass Foo {public static $ my_static = 'foo'; public function staticValue () {return self: $ my_static ;}} class Bar extends foo {public function fooStatic () {return parent ::$ my_static;} print Foo: $ my_static. "\ n"; // 'foo' $ foo = new foo (); print $ Foo-> staticValue (). "\ n"; // 'foo' print $ foo: $ my_static. "\ n"; // 'foo' print $ foo-> my_static. "\ n"; // error?>
Const
You can define a constant value in a class. You do not need to use the $ symbol when defining and using constants, but use the const symbol.
The constant value must be a fixed value, not a variable, class attribute, mathematical operation result or function call.
<?phpclass MyClass{ const constant = 'constant value'; function showConstant() { echo self::constant . "\n"; }}echo MyClass::constant . "\n";//'constant value'$classname = "MyClass";echo $classname::constant . "\n"; //'constant value'$class = new MyClass();$class->showConstant();//'constant value'echo $class::constant."\n";//'constant value'?>
This
When a method is called within the class definition, there is an available Pseudo Variable this. the reference of a special object this is a reference to this object in the member methods of the object, but it can only be used in the object member method, whether it is inside the object using $ this to access the internal members of the object. Or access members of an object through the reference name of the object outside the object, the special operator "->" is required to complete the access.
[Note] this is not available in static methods
<?phpclass A{ function foo() { if (isset($this)) { echo '$this is defined ('; echo get_class($this); echo ")\n"; } else { echo "\$this is not defined.\n"; } }}class B{ function bar() { // Note: the next line will issue a warning if E_STRICT is enabled. A::foo(); }}$a = new A();$a->foo();//$this is defined (A) A::foo();//$this is not defined. $b = new B();$b->bar();//$this is defined (B) B::bar();//$this is not defined.?>
Self
In the class method, this cannot be used to reference static variables or static methods, but self must be used to reference
<?phpclass MyClass{ const constant = 'constant value'; static function showConstant() { echo self::constant . "\n"; }}$var = new MyClass;echo $var->showConstant();//constant value?>
Parent
Parent is used to call the member methods or constants defined in the parent class.
<?phpclass MyClass{ function fn(){ echo('111'); } const A = 'a';}class Class1 extends MyClass{ function test(){ echo parent::fn().parent::A; }}$var = new Class1;$var->test();//111a?>