php - Passing an object between different classes? -


i've passed data manually between different classes. example had data produced 1 class:

$somedata = $object->somemethod(); $moredata = $object2->anothermethod($somedata); 

but feels clunky me , results in messy code gets complicated. if there multiple different kinds of data passed around multiple classes.

so instead of doing i've decided create class datacontainer groups every variable related process , pass object around different classes. passes processing pipeline, gather more , more data until every of field set value.

so example have pipeline of processing data gets modified 4 different classes - instead of passing data value pass reference:

$myobject = $class1->method1(); // class returns datacontainer object $class2->method2($myobject); $class3->method3($myobject); $class4->method4($myobject); 

is considered better choice? or there better?

keep in mind make code solid. (http://en.wikipedia.org/wiki/solid_(object-oriented_design)) in case, can create in constructor of class2 reference.

for example:

<?php class class1 {   private $class2;   public __construct(class2 $class2)   {      $this->class2 = $class2;   }    public function callmethodofclass2()   {      $value = $this->class2->getmethod();      $propertyvalue = $this->class2->public_property;   } } ?> 

or when class2 cant exists without class1, make in constructor of class1 new instance of class2 this:

<?php     class class1     {       private $class2;       public __construct()       {          $this->class2 = new class2();       }        public function callmethodofclass2()       {          $value = $this->class2->getmethod();          $propertyvalue = $this->class2->public_property;       }     }     ?> 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -