The method of static method inheritance in php: use the override function to inherit, the code is [function write(){echo 'I can be overriden!';}static function no_write()].
How to inherit static methods in php:
Before, I have been wondering whether subclasses can override static functions. , so I wrote a test code, the code is as follows:
<?php Class A { var $a='I/'m A'; function write() { echo 'I can be overriden!<br />'; } static function no_write() { echo 'Can I be overriden?<br />'; } } Class B extends A { function write() { echo 'Override from A successfully!<br />'; } static function no_write() { echo 'Can I override successful?<br />'; } } $a=new A; $a->write(); $a->no_write(); $b=new B; echo $b->a.'<br />'; //Attributes can be inherited $b->write(); $b->no_write(); //static methods can be overriden ?>
The running results are as follows:
I can be overriden! Can I be overriden? I'm A Override from A successfully! Can I override successful?
So, static functions can be overridden in subclasses, but not in java It’s so clear, I’m going to write some more code myself to test it. a
Related free learning recommendations: php programming (video)
The above is the detailed content of How to inherit static methods in php. For more information, please follow other related articles on the PHP Chinese website!