2

I am new to PHP programming and am trying out to do a basic Factory Pattern. I am trying to create a class instance using a method and also using a constructor.

$ abstract class Car {
   public $type;
   public function getType(){
   echo $this->type;
   }
  }

 //Class that holds all the details on how to make a Honda.
 class Honda extends Car{
      public  $type = "Honda";
   }

 class CarFactory {
    const HONDA = "Honda";
    public  function __construct($carType){
    switch($carType){
        case self::HONDA:
            return new Honda();
            break;
    }
    die("Car isn't recognized.");
    }

 }

 $Honda = new CarFactory(carFactory::HONDA);
 var_dump($Honda);

The result is of an object of class CarFactory. Why doesn't it create an object of type Honda as the return type is an object of type Honda? Is is because I am using a constructor?

However, if I use a method inside CarFactory as below, it creates an object of type Honda

    class CarFactory {
    const HONDA = "Honda";
      public static function createCar($carType){
        switch($carType){
            case self::HONDA:
             return new Honda();
            break;
    }
    die("Car isn't recognized.");
  }
    $carFactory = new CarFactory();
    //Create a Car
    $Honda = $carFactory->createCar(CarFactory::HONDA);
    var_dump($Honda); 

}

Thanks in advance. SV

1
  • 3
    You cant return any value from constructor, use a static method just like you use i second example Commented Sep 11, 2012 at 6:49

4 Answers 4

3
 abstract class Car {
   public static function createCar(){
        try
        { 
          // PHP 5.3+ support
          return new static;
        } 
        catch (Exception $e)
        {
           // in case child does not exists
        }
   }
} 

class Honda extends Car { 

}

$honda = Honda::createCar();
var_dump($honda);
Sign up to request clarification or add additional context in comments.

Comments

1

Constructors can't return values. They can just create an object of the class they are placed inside. you can make your second code shorter like this:

// Actually you don't need to create a new factory if your creation method is static
$Honda = $carFactory::createCar(CarFactory::HONDA);

Comments

0

In your constructor you try to return a instance of a new car. A constructor does not return any value, but is called during the creation of a object. PHP assures that the new keyword always returns a instance of the constructed class.

And because CreateCar is static, the call should look like this:

$carFactory = new CarFactory();

// Now create a Car
$Honda = $carFactory::createCar(CarFactory::HONDA);
var_dump($Honda); 

2 Comments

Thanks for the replies...Though I think it would be nice if constructor could return a value if prompted to do so...Any particular reasons for not allowing this?
Constructor cannot return values! See my post
0

What you need to do is use the static class method directly without initiating an object ($carFactory):

$honda = CarFactory::createCar(CarFactory::HONDA);

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.