【问题标题】:PHP Classes: parent/child communicationPHP 类:父/子通信
【发布时间】:2010-05-03 21:21:25
【问题描述】:

我在 PHP 中扩展类时遇到了一些问题。 已经在谷歌上搜索了一段时间。

 $a = new A();
 $a->one();

 $a->two();
 // something like this, or...

 class A {
  public $test1;

   public function one() {
    echo "this is A-one";

    $this->two();
    $parent->two();
    $parent->B->two();
    // ...how do i do something like this (prepare it for using in instance $a)?

   }
 }

 class B extends A {
   public $test2;

    public function two($test) {
     echo "this is B-two";

    }
 }

我对程序 PHP 没问题。

【问题讨论】:

  • 您能准确解释一下您完成后希望完成的工作吗?看起来您可能正在尝试以一种从未想过的方式通过继承来完成某些事情....
  • 嗨,谢谢。 - 我想从实例 a 访问函数 two()。 - 希望所有类中的 $this-> 都引用实例 a。 (+ 编辑了第三个命令)是您可以使用的描述吗?谢谢,弗兰克
  • 继承/extends 不是这样工作的,Fffff。 class B extends A 定义了一个 new 类类型,B,它具有 A 的所有功能,还包括您添加到 B 的任何新内容。它不会让A 里面有更多的东西。
  • 那么扩展意味着什么,就您如何访问它们而言?
  • 你不能“访问”它们——你创建了一个B类型的对象。 $b = new B(); 然后$b 将同时拥有$b->one()$b->two(),因为B 继承了Aone() 方法,还为B 对象添加了two() 方法。

标签: php class variables parent


【解决方案1】:

你的例子很好,但你在这里表现出一些困惑:

public function one() {
    echo "this is A-one";

    $this->two();
    $parent->two();
    $parent->B->two();
}

我想你想要的是这个:

class A
{
    function one()
    {
        echo "A is running one\n";
        $this->two();
    }
    function two()
    {
        echo "A is running two\n";
    }

}

class B extends A
{
    function two()
    {
        echo "B is running two\n";
    }
}

然后你想创建一个 B 类型的对象并调用函数“one”

$myB = new B();
$b->one();

这将输出

A is running one
B is running two

这是一个多态类行为的例子。超类将知道调用当前实例版本的函数“two”。这是 PHP 和大多数面向对象语言的标准特性。

请注意,超类永远不知道子类,您可以调用“two”方法并让 B 的版本运行的唯一原因是因为函数“two”是在父 (A) 类中定义的。

【讨论】:

    【解决方案2】:

    这是不可能的。首先,A 类是 B 类的父类,所以使用带有父类的东西是不合适的。

    子类有很多东西适用于父类:

    • B 类需要 A 存在才能工作
    • B 类可以做 A 可以做的所有事情,而且更多
    • B 类可以访问(只要允许访问)A 类的所有数据

    这些事情都不是反过来的,所以它们一起构成了你不能调用子函数的原因。

    【讨论】:

    • 谢谢。所以这意味着创建一个大的父类是正常的做法,它有很多调用其他包含类中的函数的函数?
    • 不太……你应该试试 $b = new B(); $b->one();也许这行得通的事实能够向你解释你误解了什么。
    【解决方案3】:

    仔细阅读 PHP 手册的Object Inheritance section。是的,http://us2.php.net/oop 中有很多信息,但它可能会帮助您思考您可以从OOP 中得到什么。

    【讨论】:

      【解决方案4】:

      这是你可以做的:

      class A{
        public function methodOfA (){
          echo "this is a method of A (and therefore also of B)";
        }
      }
      
      class B extends A{
        public function methodOfB (){
          echo "this is a method of B";
          // you can do {$this->methodOfA ()} if you want because all of A is inherited by B
        }
      }
      
      $a = new A ();  // $a is an A
      $a->methodOfA ();     // this is OK because $a is an A
      // can't do {$a->methodOfB ()} because $a is not a B
      
      $b = new B ();  // $b is a B, and it is also an A, because B extends A
      $b->methodOfB (); // ok because $b is a B
      $b->methodOfA (); // ok becuase $b is an A
      

      当然,还有更多。 php 手册中有一个很好的 OOP 部分(在 artlung 的回答中)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-12-19
        • 2017-12-26
        • 2018-12-22
        • 2017-03-14
        • 2018-05-08
        • 2021-10-04
        • 2023-03-11
        • 1970-01-01
        相关资源
        最近更新 更多