php-static method inheritance and other analysis

Source: Internet
Author: User

<?PHPclassA {ConstConst_name = ' A ';  Public Static $static _name= ' A ';  Public Static $static _name_no_rewrite= ' A '; Private Static $_instance;  Public Static functionStatic_func () {EchoA; }                 Public Static functionStatic_func_no_rewrite () {EchoA; }                 Public Static functionstatic_extends_self () {EchoSelf::$static _name; }                 Public Static functionstatic_extends_no_rewrite_self () {EchoSelf::$static _name; }                 Public Static functionStatic_extends_no_vars () {returnSelf::$static _no_vars; }                 Public Static functionget_instance () {if(Empty(Self::$_instance) ) { self::$_instance=NewSelf (); }            returnSelf::$_instance; }    }        classBextendsA {ConstConst_name = ' B ';  Public Static $static _name= ' B ';  Public Static $static _no_vars= ' B '; //rewrite         Public Static functionStatic_func () {EchoB; }                 Public Static functionstatic_extends_self () {EchoSelf::$static _name; }    }    //whether or not the inheritance is a reference to a parent class//1. Viewing the initial situation of the parent class and subclasses    $str= ' 1. View the initial condition of the parent class and subclass '; Echo' ; Echo $str, ' <br/> '; Echo' A::const_name-> ', a::const_name, ' <br/> '; Echo' B::const_name-> ', b::const_name, ' <br/> '; Echo' A:: $static _name-> ', A::$static _name, ' <br/> '; Echo' B:: $static _name-> ', B::$static _name, ' <br/> '; Echo' B:: $static _name_no_rewrite-> ', B::$static _name_no_rewrite, ' <br/> '; Echo' B:: $static _name_no_rewrite-> ', B::$static _name_no_rewrite, ' <br/> '; Echo' Conclusion: static attributes or class constants can be inherited <br/> '; Echo' ; //2. Testing a redefined Class property modifying the value of a subclass (parent class) If the value of the parent class (subclass) Changes    $str= ' 2. Test redefined class Properties modify the value of the subclass (parent class) to the value of the parent class (subclass) will change '; Echo $str, ' <br/> '; A::$static _name= ' M_a '; Echo"Modify->a::\ $static _name = ' m_a ' <br/>"; Echo' A:: $static _name-> ', A::$static _name, ' <br/> '; Echo' B:: $static _name-> ', B::$static _name, ' <br/> '; B::$static _name= ' M_b '; Echo"Modify->b::\ $static _name = ' m_b ' <br/>"; Echo' A:: $static _name-> ', A::$static _name, ' <br/> '; Echo' B:: $static _name-> ', B::$static _name, ' <br/> '; Echo' Conclusion: The two classes of the inheritance relationship, if the subclass redefined the static property of the parent class, then modifying the static property or constant of the parent class (subclass) does not affect the corresponding static property of the subclass (parent class) <br/> '; Echo' ; //3. Test for class properties that do not have a redefinition modify the value of the child class (parent class) If the value of the parent class (subclass) Changes    $str= ' 3. Test no redefined class properties modify the value of the child class (the parent class) whether the value of the parent class (subclass) will change '; Echo $str, ' <br/> '; Echo' A:: $static _name_no_rewrite-> ', A::$static _name_no_rewrite, ' <br/> '; Echo' B:: $static _name_no_rewrite-> ', B::$static _name_no_rewrite, ' <br/> '; A::$static _name_no_rewrite= ' M_a '; Echo"Modify->a::\ $static _name_no_rewrite = ' m_a ' <br/>"; Echo' A:: $static _name_no_rewrite-> ', A::$static _name_no_rewrite, ' <br/> '; Echo' B:: $static _name_no_rewrite-> ', B::$static _name_no_rewrite, ' <br/> '; B::$static _name_no_rewrite= ' M_b '; Echo"Modify->b::\ $static _name_no_rewrite = ' m_b ' <br/>"; Echo' A:: $static _name_no_rewrite-> ', A::$static _name_no_rewrite, ' <br/> '; Echo' B:: $static _name_no_rewrite-> ', B::$static _name_no_rewrite, ' <br/> '; Echo' Conclusion: If a subclass does not redefine a static property of the parent class, then the corresponding static property of the subclass (parent class) is modified <br/> '; Echo' Deeper conclusion: the subclass inherits the parent class with a reference to the parent class that blocks the memory, and if the subclass does not redefine a property (placed in this class of memory), it invokes the parent class's property <br/> '; Echo' ; //4. Whether static methods can inherit    $str= ' 4. Whether static methods can inherit '; Echo $str, ' <br/> '; Echo' A::static_func_no_rewrite () ', A::static_func_no_rewrite (), ' <br/> '; Echo' B::static_func_no_rewrite () ', B::static_func_no_rewrite (), ' <br/> '; Echo' Conclusion: Static methods can inherit! ‘; Echo' ; //5. Whether static methods can be overridden    $str= ' 5. Whether static methods can be rewritten '; Echo $str, ' <br/> '; Echo' A::static_func () ', A::static_func (), ' <br/> '; Echo' B::static_func () ', B::static_func (), ' <br/> '; Echo' Conclusion: Static methods can be rewritten! ‘; Echo' ; //6. The parent class static method has self, the subclass inherits this method does not have to rewrite after the call self is who?     $str= ' 6. There is a self in the parent static method, and the subclass inherits this method without rewriting after the self is called who? ‘; Echo $str, ' <br/> '; Echo' A::static_extends_no_rewrite_self () ', a::static_extends_no_rewrite_self (), ' <br/> '; Echo' B::static_extends_no_rewrite_self () ', b::static_extends_no_rewrite_self (), ' <br/> '; Echo' Conclusion: the same as 3, if there is no rewrite, it is only a subclass of the parent class of a reference, if not in the subclass, go to the parent class to look for, and find is also dependent on the parent class! ‘; Echo' Deeper conclusion: in the inheritance of PHP Singleton, the parent class:: Get_instance () method returns the new self (), if the subclass is not overridden, returns an instance of the parent class! ‘; Echo' ; //7. In the parent class static method, there is self, who is the self that the subclass inherits this method override after calling?     $str= ' 7. There is a self in the parent static method, who is the self that the subclass inherits this method override after calling? ‘; Echo $str, ' <br/> '; Echo' A::static_extends_self () ', a::static_extends_self (), ' <br/> '; Echo' B::static_extends_self () ', b::static_extends_self (), ' <br/> '; Echo' Conclusion: After a subclass overrides a method of the parent class, if there is a self property, then the parent class is overwritten with the own, that is, the object itself at this time '; Echo' ; //8. The parent class static method call itself has no child class but has static properties (to verify the above 6 speculation, if it is a reference, it will be empty)    $str= ' 8. The subclass calls the parent class static method call the parent class has no subclass but has a static property of self (to verify the above 6 conjecture, if it is a reference, it will be empty) '; Echo $str, ' <br/> '; //Echo ' B::static_extends_no_vars (), B::static_extends_no_vars (), ' <br/> ';    Echo' B::static_extends_no_vars (), Error <br/> '; Echo' Conclusion: program error, show a does not have this attribute, the description is a subclass of a reference to the parent class! Subclasses call the parent class if they have not overridden a property or static method of the parent class! including self! '; Echo' ; //9. Test the inference in the next 6, that is, the self in the singleton mode    $str= ' 9. Test the inference in the next 6, that is, ' self ' in singleton mode; Echo $str, ' <br/> '; Echo' Get_class (B::get_instance ()),Get_class(B::get_instance ()), ' <br/> '; Echo' Conclusion: The deeper inference in note 6 is correct! ‘; Echo' ; //10. Test instance calls a static property or constant    $str= ' Test instance calls a static property or constant '; Echo $str, ' <br/> '; $a=NewA (); //echo ' $a = new A (), echo $a->const_name-> ', $a->const_name, ' <br/> ';    Echo' $a = new A (); Echo $a->const_name-> error <br/> '; //Echo ' echo $a $static _name-> ', $a-$static _name, ' <br/> ';    Echo' Echo $a $static _name-> error <br/> '; Echo' Conclusion: an instance cannot invoke a static property or constant of a class! Calling a static property turns into a variable, and calling the constant invokes the corresponding property of the instance! ‘; Echo' ; //11. Test instance call static method    $str= ' Test instance calls a static property or constant '; Echo $str, ' <br/> '; Echo' $a->static_func (),$a->static_func (), ' <br/> '; Echo' $a->static_extends_self (),$a->static_extends_self (), ' <br/> '; Echo' Conclusion: Instances can call static methods, even static methods contain self! ‘; Echo' 

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.