mysql - PHP: Calling a method on a class property that is an object -


can let me know if practice or if there better cleaner way it? looks clumsy me, i'm wondering if there's reason haven't seen before.

class {   public $instance_of_b;    function __construct () {     $this->instance_of_b = new b;   }    function deeper_hello() {     $this->instance_of_b->say_hi();   } }  class b {   public function say_hi() {     echo "hello, b!";   }  }  $instance_of_a = new a; $instance_of_a->deeper_hello(); 

it parses php it's not semanticly wrong, there cleaner way of doing it?

i should say, not code, it's method. i'm doing creating sql class uses pdo. in sql class, i'm recording pdo , pdostatment objects in properties of sql class.

rather instantiating b inside of a, better pass b via constructor. called dependency injection , doing no longer tightly coupled b.

class {   public $instance_of_b;    function __construct ($b) {     $this->instance_of_b = $b;   }    function deeper_hello() {     $this->instance_of_b->say_hi();   } }  class b {   public function say_hi() {     echo "hello, b!";   }  }  $instance_of_b = new b; $instance_of_a = new a($instance_of_b); $instance_of_a->deeper_hello(); 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -