Home  >  Article  >  Backend Development  >  How to inherit static methods in php

How to inherit static methods in php

coldplay.xixi
coldplay.xixiOriginal
2020-10-06 11:46:132532browse

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

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=&#39;I/&#39;m A&#39;;
function write()
{
echo &#39;I can be overriden!<br />&#39;;
}
static function no_write()
{
echo &#39;Can I be overriden?<br />&#39;;
}
 
}
 
Class B extends A
{
function write()
{
echo &#39;Override from A successfully!<br />&#39;;
}
static function no_write()
{
echo &#39;Can I override successful?<br />&#39;;
}
}
$a=new A;
$a->write();
$a->no_write();
 
$b=new B;
echo $b->a.&#39;<br />&#39;;  //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&#39;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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn