PHP:从子类覆盖父类属性 [英] PHP: override parent class properties from child class

查看:147
本文介绍了PHP:从子类覆盖父类属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许您知道从子类方法中重写类属性的一些正确方法"

Maybe you know some "right-way" to override class property from child class method

<?php
class A {
  public $option;
  public function __construct() {
    $this->option = array(1,2,3);
  }

  public function bb() {
    $obj = new B;
    $obj->aa();

    print_r($this->option);die;
  }
}

class B extends A {

  public function __construct() {
    parent::__construct();
  }

  public function aa() {
    $this->option[] = 4;
    //print_r($this->option);die;
  }
}

$obj3 = new A;
$obj3->bb();
?>

返回Array ( [0] => 1 [1] => 2 [2] => 3 )且方法aa()

是否可以从子类中覆盖父类属性?

Is it possible to override parent class property from child class?

推荐答案

问题是您正在尝试以某种根本无法完成的方式来做某事.很难说出您要做什么,但希望如果您看一下我的样本,它将告诉您正在发生的事情:

The problem is that you are trying to do something in a way it is not at all meant to be done. It is a little difficult to tell what you are trying to do but hopefully if you look at my sample it will show you what is happening:

<?php

class BaseClass {
    /* If a property is public or protected it can be overriden in derived classes.
     * If you had made it private it wouldn't be changable in derived classes. 
     * If you make it also make it static then there is only one copy of it shared 
     * by all instances of the class (as well as those derived from it). */
    protected $option=array();

    public function __construct() {
        $this->option=array(1,2,3);
        /* We are in the base class so the reset() method doesn't exist yet.
         * This fails in this class: */
        // $this->reset();
    }
    /** Print the $option variable. */
    public function dump() {
        echo sprintf('<pre><b>%s</b> = ', get_class($this));
        print_r($this->option);
        echo "</pre>\n";
    }
}

class DerivedClass extends BaseClass {
    public function __construct() {
        parent::__construct();
        /* We have complete control over the variable BaseClass::$option from this class.
         * If we want to we can even make it a string instead of an array. */
        $this->option="I've overridden the option property.";
    }
    /** Reset the BaseClass::$option variable. */
    public function reset() {
        $this->option=array(1,2,3,4);
    }
}

/// Create a few test objects to play with
$objBase=new BaseClass();
$objDerived=new DerivedClass();

$objBase->dump(); // Display the base object data

/// If BaseClass::$option is public you can edit it directly here.
/// This may be easier but it is discouraged because it breaks OOP principals.
//$objDerived->option[]=5;

$objDerived->dump(); // Display the derived object data

$objDerived->reset(); // call a method which will change our data
$objDerived->dump(); // Display the derived object data again now

类提供了许多方法来进一步控制那些从当前类派生并从外部派生的类所看到的内容.这可以通过使用public,protected和private关键字来完成.设置为public时,方法和属性对所有对象均可见,但它们仍可能超出范围.简而言之,您遇到的唯一问题是方法B:aa()超出了A :: bb()的范围.

Classes give many ways to further control what can be seen by those classes derived from the current class and externally. This is done by using the public, protected and private keywords. When set to public, methods and properties are visible to everything but they can still be out of scope. In short, the only problem you had was that your method B:aa() was out of scope in A::bb().

请记住,实际上,BaseClass可能与DerivedClass不在同一个文件中,并且某些项目可能不需要DerivedClass.如果您牢记这一点,那就应该避免使用甚至没有加载到项目中的方法.

Remember that in reality BaseClass may not be in the same file as DerivedClass and some projects may not need DerivedClass. If you keep that in mind it should make sense that you can't use methods which aren't even being loaded into the project.

在您的代码中,您将使用A :: bb()方法创建B的全新实例.这是一种设计模式,除了一些特定类型的函数(称为工厂函数,因为它们旨在有意从基类内部创建派生类)之外,您几乎不会看到它.我相当确定这不是您要在此处执行的操作,因为您通常不会在PHP中执行此操作,因为PHP通常不会从文件中读取二进制类数据,而在其他情况下,通常会有更好的方法来完成任务.

In your code you are making a whole new instance of B in the method A::bb(). This is a design pattern which you will almost never see except in a few specific types of functions (called factory functions because they are meant for creating derived classes from within a base class on purpose). I'm fairly sure that is not what you are doing here because that is not something you do in PHP very often because PHP doesn't usually read binary class data from files and in other situations there are usually better ways to get things done.

这篇关于PHP:从子类覆盖父类属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆