簡體   English   中英

從擴展的公共功能類訪問公共屬性父類

[英]Access public properties parent class from an extended public function class

我是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代碼,
我是新手,請嘗試練習:)
該代碼無效,
打印出錯誤


注意 :未定義的變量:第11行的xxx.php中的寬度

注意 :未定義變量:第11行上xxx.php中的長度

注意 :未定義的變量:第11行的xxx.php中的高度
直角棱鏡體積為:0米立方
注意 :未定義的變量:第23行上xxx.php中的寬度

注意 :未定義的變量:第23行上xxx.php中的長度

注意 :未定義的變量:第23行上xxx.php中的高度
直角棱鏡體積為:0米立方

我想問的是如何獲取該父類的屬性
在一個函數上,並分配一個新的屬性值來覆蓋寬度,長度,高度,屬性值
感謝您為我提供幫助的人:)

這是您嘗試做的更好的方法,使用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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM