【问题标题】:Access public properties parent class from an extended public function class从扩展的公共函数类访问公共属性父类
【发布时间】:2014-01-26 14:47:14
【问题描述】:

我是 PHP 新手,这是我的代码

class number_config{
    public $length = 200, //declare a properties
           $width = 100,
           $height = 300;
}

class formula extends number_config{
        public function rectangular_prism(){
            $volume = $length * $width * $height;
            echo 'The rectangular prism volume is: '. $volume .'meter cubic';
         // i want this echo produce this,
         // The rectangular prism volume is: 6000000 meter cubic

         //set the new member_config properties value
            $length = 600;
            $width = 1000;
            $height = 400;
        }

        public function rectangular_prism2(){
            $volume = $length * $width * $height;
            echo 'The rectangular prism volume is: '. $volume .'meter cubic';
         // i want this echo produce this,
         // The rectangular prism volume is: 240000000 meter cubic

      //and then set the other new member_config properties value, and so on
            $length = 800;
            $width = 2000;
            $height = 300;
        }

}
$rectangular_prism_volume = new formula();
echo $rectangular_prism_volume->rectangular_prism();
echo $rectangular_prism_volume->rectangular_prism2();

首先,我很抱歉这个愚蠢的 php 代码,
我是新手,只是尝试练习:)
该代码不起作用,
它打印出一个错误


通知:未定义变量:xxx.php 中的宽度在第 11

通知:未定义变量:xxx.php11 行的长度

注意:未定义变量:高度xxx.php11 行
矩形棱柱体积为:0 米立方
注意:未定义变量:xxx.php23

注意:未定义变量:xxx.php 中的长度,第 23

注意:未定义变量:xxx.php 中的高度 23
长方体体积为:0米立方

我想问的是如何获取该父类的属性
在函数上,并分配一个新的属性值,覆盖宽度、长度、高度、属性值
感谢任何帮助我处理此案的人:)

【问题讨论】:

  • 使用$this->width等从类的实例中访问对象属性
  • 哇.. 真是太快了.. :D 谢谢你的帮助,它解决了一切,即使我使用的是受保护的属性 :D

标签: php function class properties public


【解决方案1】:

这是一种更好的方法,可以在需要时使用 set 和 get 方法设置成员值,然后获取它。

class number_config{
    public  $length = 200; //declare a properties
    public  $width = 100;
    public  $height = 300;

        public function setLength($l){
             $this->length = $l;
        }

        public function setWidth($w){
             $this->width = $w;
        }

        public function setHeight($h){
             $this->height = $h;
        }

        public function getLength(){
            return $this->length ;
        }

        public function getWidth(){
            return $this->width ;
        }

        public function getHeight(){
            return $this->height ;
        }

}

class formula extends number_config{
        public function rectangular_prism(){
            $volume = $this->getLength() * $this->getWidth() * $this->getHeight();
            echo 'The rectangular prism volume is: '. $volume .'meter cubic';
        }

        public function rectangular_prism2(){
            $volume = $this->getLength() * $this->getWidth() * $this->getHeight();
            echo 'The rectangular prism volume is: '. $volume .'meter cubic';

        }

}

$formula = new formula();

//call the function rectangular_prism() with some values
$formula->setLength(600);
$formula->setWidth(1000);
$formula->setHeight(400);
$formula->rectangular_prism();

//call the function rectangular_prism2() with some values
$formula->setLength(800);
$formula->setWidth(2000);
$formula->setHeight(300);
$formula->rectangular_prism2();

【讨论】:

  • 很高兴看到您的代码,但不幸的是,我现在不仅使用了 3 个属性,而且还使用了 20 个或更多属性。而且我不想将值传递给函数参数:)如果你有更好的代码,那就给我看看;)
  • 我看看你是否有更多的属性,并且你希望它们在运行中使用,那么最好的方法是使用“object->property = val”,因为正如你所说的那样它可能超过 20 个。以上是说明代码流程的示例。但是,您可以看到像 __get() 和 __set() 这样的魔术方法,当您不知道您可能拥有多少属性并且可以用于动态设置和获取时,它们很有用。
猜你喜欢
  • 2011-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-17
  • 2012-09-03
  • 1970-01-01
  • 1970-01-01
  • 2011-12-23
相关资源
最近更新 更多