在子方法中访问父变量

发布于 2024-08-06 21:44:34 字数 546 浏览 2 评论 0原文

我目前有两个类,一类称为“狗”,一类称为“贵宾犬”。现在我如何使用 Poodle 类中 Dog 中定义的变量。我的代码如下:

  class dog {
       protected static $name = '';

       function __construct($name) {
            $this->name = $name
       }
  }

  class Poodle extends dog {
       function __construct($name) {
           parent::__construct($name)
       } 

       function getName(){
           return parent::$name;
       }
  }

$poodle = new Poodle("Benjy");
print $poodle->getName();

我收到此错误

注意:未定义的变量:名称

I currently have two classes, one called Dog, one called Poodle. Now how can I use a variable defined in Dog from the Poodle class. My code is as follows:

  class dog {
       protected static $name = '';

       function __construct($name) {
            $this->name = $name
       }
  }

  class Poodle extends dog {
       function __construct($name) {
           parent::__construct($name)
       } 

       function getName(){
           return parent::$name;
       }
  }

$poodle = new Poodle("Benjy");
print $poodle->getName();

I get this error

Notice: Undefined variable: name

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

枯叶蝶 2024-08-13 21:44:34

我猜“名字”是具体狗的一个属性,所以它首先不应该是静态的。要从继承类中访问非静态父类属性,只需使用“$this”。

    class dog {
       protected $name = '';

       function __construct($name) {
            $this->name = $name;
       }
    }

    class Poodle extends dog {
       function getName(){
           return $this->name;
       }
    }

i guess 'name' is an attribute of the concrete Dog, so it shouldn't be static in the first place. To access non-static parent class attributes from within an inherited class, just use "$this".

    class dog {
       protected $name = '';

       function __construct($name) {
            $this->name = $name;
       }
    }

    class Poodle extends dog {
       function getName(){
           return $this->name;
       }
    }
幸福丶如此 2024-08-13 21:44:34

问题出在您的 Dog 构造函数中。您写道:

$this->name = $name;

但是使用 $this 意味着 name 是一个实例变量,而实际上它是一个静态变量。将其更改为:

self::$name = $name;

应该可以正常工作。

The problem is in your Dog constructor. You wrote:

$this->name = $name;

But using $this implies that name is an instance variable, when in fact it's a static variable. Change it to this:

self::$name = $name;

That should work fine.

笑梦风尘 2024-08-13 21:44:34

在您的狗类中,您已将变量 $name 声明为 static,您必须在声明变量时不使用 static 一词

class dog {
   protected $name = '';

   function __construct($name) {
        $this->name = $name
   }
}



class Poodle extends dog {
   function __construct($name) {
       parent::__construct($name)
   } 

   function getName(){
       return $this->name;
   }
}

$poodle = new Poodle("Benjy");
print $poodle->getName();

In your dog class you have declared the variable $name as static, you have to declare the variable without the static word

class dog {
   protected $name = '';

   function __construct($name) {
        $this->name = $name
   }
}



class Poodle extends dog {
   function __construct($name) {
       parent::__construct($name)
   } 

   function getName(){
       return $this->name;
   }
}

$poodle = new Poodle("Benjy");
print $poodle->getName();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文