2016-05-24 44 views
0

你好家伙我想在这里一些帮助在父类中调用如何变量

我有这个类:

class product 
     { 
      var $title; 
      var $price; 
      function __construct($title1,$price1) 
      { 

      $this->title=$title1; 
      $this->price=$price1; 
      } 
      function set_title($newtitle){ 
      $this->title=$newtitle; 
      } 
      function get_title() { 
      return $this->title; 
      } 
      function set_price($newprice) 
      { 
      return $this->price=$newprice; 

      } 
      function getProductAtrribute() 
      { 

      $x1=$this->title; 
      $x2=$this->price; 
      $x3=$this->**material**; 
      $x4=$this->**size1**; 
      return $allattribute= array("$x1","$x2","x3","x4"); 
     } 
     } 

和其他班组长:

class furinture extends product 
    { 
     var $size; 
     var $material; 

     function __construct($sizeset,$materialset) 
     { 
     parent::__construct(); 

     $this->size=$sizeset; 
     $this->material=$materialset; 
     } 
    function setsize ($size1,$material1) 
    { 
     $this->size=$size1; 
     $this->material=$material1; 
    } 

我试图做我设置的大小和材料的类家具的价值,但从功能得到所有属性在类产品我试图使全局变量,但它不会工作。
注意:$ size和$ material我希望他们定义类似于其定义的家具类

+0

http://stackoverflow.com/questions/6456939/php-accessing-parent-class-variable –

+0

这如何访问父类中我需要访问子类 –

+0

http://stackoverflow.com/questions/13774612/access-child-class-static-variables-from-parent-class –

回答

-1

使用公共,私有或保护您的属性而不是var。 那么你可以从$this->price;等访问它...

或者你可以做accesor调用它与parent :: yourMethod();

+0

但我想访问的是子类中的大小和材质,但在代码中看到的类产品中获取它,以及在功能getallattribute,我试过公共材料,公共大小,它不工作 –

+0

使用'var'把范围反正'公开' –

+0

你可以尝试使用抽象类 –

0

修改你的产品类这样的,

class product 
    { 
     private $title; 
     private $price; 
     private $size; 
     private $material; 

     public function setup_product($title1,$price1) 
     { 
     $this->title=$title1; 
     $this->price=$price1; 
     } 

     public function set_title($newtitle){ 
     $this->title=$newtitle; 
     } 

     public function set_material($material){ 
     $this->material=$material; 
     } 

     public function set_size($size){ 
     $this->size=$size; 
     } 

     public function get_title() { 
     return $this->title; 
     } 

     public function get_price() { 
     return $this->price; 
     } 

     public function set_price($newprice) 
     { 
     return $this->price=$newprice; 

     } 

     public function getProductAtrribute() 
     { 

     $x1=$this->title; 
     $x2=$this->price; 
     $x3=$this->material; 
     $x4=$this->size; 
     return $allattribute= array("$x1","$x2","x3","x4"); 
    } 
    } 

修改家具类;

class furinture extends product 
{ 
    public function __construct($sizeset,$materialset,$title1,$price1) 
    { 
    $this->setup_product($title1,$price1) 
    $this->size=$sizeset; 
    $this->material=$materialset; 
    } 
    public function setsize($size1,$material1) 
    { 
    $this->set_size($size1); 
    $this->set_material($material1); 
    } 

} 
+0

我不想从儿童类的价格,我想从父Cla得到尺寸和材料ss函数getallattribute()。如你所见,我得到x3和x 4一个用于matrieal和其他大小 –

+0

我已经扩展了上面的家具类现在返回'allAttributes()函数' – guyver4mk

+0

我同意你的意见但仍然没有帮助。请阅读函数getallattribute。 x3和x4。 –