Jump to content

Is it ok to have arrays of objects as a private property?


diegueins

Recommended Posts

I need to create an object of type class1 that as one ot its properties has an array of objects of type class2. Class2 objects also need to have a property that is an array of objects of class3. For some reason, my code is not working as I'd expect... Am I using constructors and destructors properly? The structure of my program is similar to the next:

 

for(some condition){

    $object1 = new Class1($arguments);

    $object1->SomeOperation();

}

$pnr->__destruct();

 

class Class1

{

    private static $properties; //which will be an array of class2 objects

    public function __construct($p){

        $this->setProperties($p);

    }

    public function __destruct(){

        self::$properties = null;

    }

    public function setProperties($args) {

        $i = 0:

        while(some condition){

            $class2object = new Class2($arguments);

            self::$registros[$i] = $nuevoRegistro;

            $i++;

        }

    }

}

       

class Class2

{

    private static $properties; //which will be an array of class2 objects

    public function __construct($p){

        $this->setProperties($p);

    }

    public function __destruct(){

        self::$properties = null;

    }

    public function setProperties($args) {

        $i = 0:

        while(some condition){

            $class2object = new Class2($arguments);

            self::$registros[$i] = $nuevoRegistro;

            $i++;

        }

    }

}

 

class Class3

{

    private static $properties; //which will be an array of class2 objects

    public function __construct($p){

        $this->setProperties($p);

    }

    public function __destruct(){

        self::$properties = null;

    }

}

 

Plese, do help me!

don't use static.. you're misusing the static keyword in your examples.. removing the static keywords will resolve all your problems. you can't use a static declaration through an instantiation, you'd need to use it through the object itself with the scope resolution operator (::)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.