Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Passing an instance of a class to another class

I'm still kind of new to PHP OO programming techniques and I have a very simple broad question, is it generally bad practice to instantiate a class in a class then pass that instance to another class?

What I want is to be able to create an instance of a specific class I know I will always need through each user request. Class two is more than anything just a helper class, ideally in my application it would be a "loader" that loads the views.

First class which calls the other two classes:

require 'classTwo.php';
require 'classThree.php';

class first {
     public $classTwo, $classThree;

     public function __construct() {
          $this -> classTwo = new classTwo;
          $this -> classThree = new classThree;

          $this -> classThree -> displayNum( $this -> classTwo );

     }
}

Second class which is the helper class:

class classTwo {
     public function returnVal() {
          return 123;
     }
}

Third class is the action class where actual work and final display would happen:

class classThree {
     public function displayNum( $instance ) {
          echo $instance -> returnVal();
     }
}

Overall I just want to know if this is bad practice because I never seen it done before.

Thanks in advance!

like image 275
Tom Bird Avatar asked Mar 16 '13 00:03

Tom Bird


People also ask

How can use class from another class in PHP?

In PHP, classes are declared with the class command while objects are declared with the new keyword. Inheritance is initialized using the extends keyword. Inheritance is a concept in PHP OOP, where classes are created out of another class. Simply put, it means to derive one class from another.

What is parent__ construct?

We can do this by using the special function call parent::__construct(). The "parent" part means "get the parent of this object, and use it", and the __construct() part means "call the construct function", of course. So the whole line means "get the parent of this object then call its constructor".

What is a destructor PHP?

A destructor is called when the object is destructed or the script is stopped or exited. If you create a __destruct() function, PHP will automatically call this function at the end of the script. Notice that the destruct function starts with two underscores (__)!

What is an instance of a class in PHP?

An instance is an object that has been created from an existing class. Creating an object from an existing class is called instantiating the object. To create an object out of a class, the new keyword must be used. Classes should be defined prior to instantiation.


2 Answers

It is good practice to inject any dependent objects, as this allows you to mock quite easily during unit testing.

Generally speaking, if you have ClassA and it relies on an instance of ClassB to function then you could do something like..

$objectB = new ClassB();
$objectA = new ClassA($objectB);

Take a look at http://net.tutsplus.com/tutorials/php/dependency-injection-in-php/

EDIT: Constructor injection is passing the dependency into the new object's constructor (as demonstrated above).

Setter injection is using a method to set the dependency, and is usually utilised when the dependency isn't critical:

$objectB = new ClassB();
$objectA = new ClassA();
$objectA->setObjectB($objectB);
like image 67
JamesHalsall Avatar answered Oct 01 '22 18:10

JamesHalsall


Yes. This would make it a pain to unit test your first class because there are dependencies hard coded in it.

Either inject the instances of the classes or inject a factory.

like image 33
PeeHaa Avatar answered Oct 01 '22 16:10

PeeHaa