Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8512615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:19:41+00:00 2026-06-11T04:19:41+00:00

I am new to PHP programming and am trying out to do a basic

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T04:19:43+00:00Added an answer on June 11, 2026 at 4:19 am

    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); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to object oriented programming in PHP. I included a class and called
I am new to programming PHP and am trying to validate a field in
I am working on something. I am trying to Create a class in config.php
I'm new to Php/Jquery Programming, and i past all this day trying figuring how
After 4 years of absence in PHP programming I'm trying to make some new
I'm new to PHP and MySQL (and programming in general) and I'm trying to
Very new to PHP/programming in general, and I've been trying to run a PHP
First off, I'm very new to PHP (and programming in general) and I'm trying
hello I'm new to PHP programming and I migrated from ASP .net to PHP..
I'm new to PHP programming and I wanted to know that is it possible

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.