10

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!

2
  • 2
    Upvoted since it's a good question to ask and I wished I had asked such questions when I first started OOP. Once your familiar with dependency injection which the link in Jaitsu's answer covers well, I recommend taking a look at Pimple, which is a DI container for all your classes and parameters. Commented Mar 16, 2013 at 0:51
  • Thanks @Adam-E I definitely agree and I've worked and read enough PHP OO techniques and never seen the method I was using before and I thought who better to ask than Stack Overflow users which I always find very helpful. I'll definitely look into Pimple thanks! Commented Mar 16, 2013 at 1:14

2 Answers 2

14

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot @Jaitsu, Can you tell me though why is it any better to pass the instance from instantiation rather than from a method.
@TomBird There are two types of injection, setter injection and constructor injection. Usually if the dependency is optional then you use setter injection, but if your object must receive an instance of another object in order to work then use constructor injection
4

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.