If you are a lazy programmer, you may be annoyed with the following code: abstractclassU {} classu1extendsU {publicstaticfunctioncreate () {returnnewu1 () ;}} classu2extendsU {publicstatic... "/> If you are a lazy programmer, you may be annoyed by the following code: abstract class U {} class u1 extends U {public static function create () {return new u1 () ;}} class u2 extends U {public static function create () {return new u2 () ;}} this code works properly, but a large number of repeated code will be annoying. I don't want to add the create method to each subclass. if you put the create method in the Super Class U, the code may be AB Stract class U {public static function create () {return new self () ;}} class u1 extends U {function a () {}} class u2 extends U {} u1 :: create (); looks elegant and neat. now we place common code in one place and use self as a reference to this class. But here we make a hypothesis for self. In fact, the role of self on this class is not exactly the same as that of $ this on the object. Self is not the call context, but the parsing context. Therefore, if you run the column above, you will get the Fatal error: Cannot instantiate abstract class U in D: \ wamp \ www \ test \ oop \ static. php on line 21, so self is parsed to define the create U instead of the u1 class that calls self. Before php5.3, there were strict restrictions in this regard and many clumsy solutions were created. php5.3 introduced delayed static binding and the keyword static is similar to self, but it refers to the called class rather than the containing class. In the following example, u1: create will generate the u1 object instead of instantiating the U object abstract class U {public static function create () {return new static ();}} class u1 extends U {} class u2 extends U {} u1: create (); static can be used not only for instantiation, but also as a static method call identifier like self and parent, even calling abstract class U {private $ group; public function _ construct () {$ this-> group = static: getGroup ();} from a non-static context ();} public static function create () {return new static ();} static function getGroup () {return 'default ';}} class u1 extends U {} class u2 extends U {static function getGroup () {return 'U2 ';} class u3 extends u2 {} print_r (u1: create ()); echo'
'; Print_r (u3: create (); u1 Object ([group: U: private] => default) u3 Object ([group: U: private] => u2)
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