Introduction to PHP 8 Object-oriented 1 Overriding methods and properties Accessing parent classes

PHP中文网
Release: 2016-07-28 08:26:20
Original
1000 people have browsed it

1. Subclasses cannot access the private properties and methods of the parent class

2. Overloading of methods is not supported

<?phpclass _Parent{
    //子类中无法访问父类private属性 protected可以
    private  $data = "parent";    protected   $data2 = "protected data";    public function _print()
    {
        echo $this->data . "<br>";
    }
}class childA extends _Parent{}class childB extends _Parent{

    protected $data = "childB"; // 覆盖父类的属性
                              // 覆盖父类的方法
    public function _print()
    {
        echo $this->data . "<br>";
    }
}class childC extends _Parent{
    public  function  other(){
       // echo  $this->data; 错误 不能访问父中 private属性
        echo  $this->data2;
    }    //错误  无法重载方法 若要重写  必须参数和返回值相同
    public  function _print($d)
    {
        echo  $this->data2 . $d;
    }
}
$A = new childA();
$B = new childB();
$C = new childC();
$A->_print();
$B->_print();
$C->other();
//错误 PHP不支持方法的重载
$C->_print(",hi");
?>
Copy after login

The above is PHP Introduction 8 Object Oriented 1 Methods and Properties Override Access to the Contents of the Parent Class, More Related Please pay attention to the PHP Chinese website (www.php.cn) for content!


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!