$ THIS, SELF, PARENT, STATIC Const

xiaoxiao2021-03-06  32

About the use of $ this, self, parent, static constings in PHP5, is clarified. Const, $ this, self static use and distinguishing Code:

Const, $ this, Self Static Application and Difference

CODE:

Class foo {const tmp = 'this is const tmp = 0; private $ tmp2 = 1; function __construct () {print self: $ tmp1; // print self: $ tmp2; // error $ tmp2 is not static // print $ this-> tmp1; // null $ this-> tmp1 is null, tmp1 is static print $ this-> tmp2; print self :: tmp; // print 'this is const tmp } Static function test () {print "this is static fun";}}

Print foo :: tmp; // print const (); // print test () OUT; $ obj = new foo; $ obj-> test (); // this is fun

PARENT is mainly used on extended CLASS,

CODE:

TMP1; // Null $ this-> TMP1 IS NULL, TMP1 IS Static Print $ this-> TMP2; Print Self :: Tmp; // Print 'this is const TMP '} static function test () {print "this is static fun";}}

Class foo2 extends foo {

Function __construct () {parent :: __ construct (); // __ connStruct used parent :: test (); // print static fun; print parent :: tmp; //print const // print parent :: tmp1; // error }}} print foo :: tmp; // print const (); // print test () OUT; $ obj = new foo; $ obj-> test (); // this is fun

Print "/ n Test Parent / N";

$ OBJ2 = New Foo2 ();

?> ¢

转载请注明原文地址:https://www.9cbs.com/read-82659.html

New Post(0)