dongpiansui8755 2014-02-13 08:36
浏览 38
已采纳

对象类实例化数组[关闭]

Hello I'm having some trouble creating an array that should hold all the instances of my class. For example I'd like that everytime I instantiate an object from my main class to automatically put that object in an empty array created previously in the class.

    class Meniu_de_baza{   
        private $nume;
        private $descriere_ro;
        private $descriere_en;
        private $pret;
        private $array_obiecte[];

        function __construct($n,$d_ro,$d_en,$p){
            $this->nume = $n;
            $this->descriere_ro = $d_ro;
            $this->descriere_en = $d_en;
            $this->pret = $p;
            $this-array_obiecte = $this-

        }

        function getPret(){
            return $this->pret;
        }

        function setPret($set_pret){
            if(is_numeric($set_pret)){
                $this->pret = $set_pret;
            }else{
                echo "Pretul este invalid!";
            }
        }

        function detalii_mancare(){
            echo "Ati comandat {$this->nume} ce contine: <br />" . "{$this->descriere_ro} <br />";
        }

        function nota_plata(){
            echo "Nota Plata: <br />{$this->nume} 1 portie <br /> TOTAL: {$this->pret} RON";
        }
}

The code is attached as an image due to an issue I've occured trying to put it in a code block. Thanks in advance. Any advice is appreciated.Here is the class

  • 写回答

1条回答 默认 最新

  • drau89457 2014-02-13 08:41
    关注

    small example

    class Sample{
        private static $objects=array();
    
         public function __construct()
        {
            static::$objects[]=$this;
        }
    
        public function getObjects(){
             return static::$objects;
        }
    
    }
    

    declare that field as static so variable will be at class level not at object level.

    How to use:

    new Sample();
    new Sample();
    new Sample();
    
    $a=new Sample();
    
    var_dump($a->getObjects());
    

    Hope this might help you

    EDIT FOR YOUR CODE:

    class Meniu_de_baza{   
            private $nume;
            private $descriere_ro;
            private $descriere_en;
            private $pret;
            private static $array_obiecte[];
    
            function __construct($n,$d_ro,$d_en,$p){
                $this->nume = $n;
                $this->descriere_ro = $d_ro;
                $this->descriere_en = $d_en;
                $this->pret = $p;
                static::$array_obiecte[] = $this;
    
            }
    
            function getPret(){
                return $this->pret;
            }
    
            function setPret($set_pret){
                if(is_numeric($set_pret)){
                    $this->pret = $set_pret;
                }else{
                    echo "Pretul este invalid!";
                }
            }
    
            function detalii_mancare(){
                echo "Ati comandat {$this->nume} ce contine: <br />" . "{$this->descriere_ro} <br />";
            }
    
            function nota_plata(){
                echo "Nota Plata: <br />{$this->nume} 1 portie <br /> TOTAL: {$this->pret} RON";
            }
    

    Problem with your code :

    you forgot semicolon at static::$array_obiecte = $this;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!