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 1013229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:04:09+00:00 2026-05-16T10:04:09+00:00

I have a class that has a complex static factory method, lets call it

  • 0

I have a class that has a complex static factory method, lets call it ClassA. I have extended ClassA into ClassB and I want ClassB’s factory method to do everything that ClassA’s factory method does, except return a ClassB.

class ClassA{
   static public function Factory($construct_args, $contents){
      $new = new ClassA($construct_args);
      //Does lots of stuff with $contents
   }
}

class ClassB extends ClassA{
   //specialty methods and properties
}

echo get_class(ClassB::Factory($construct_args, $contents);

This should echo ClassB

Is there a way I can use ClassA’s Factory Method without copy-pasting it to ClassB? I am using PHP5, but not 5.3.0.

  • 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-05-16T10:04:10+00:00Added an answer on May 16, 2026 at 10:04 am

    To do exactly what you want you need Late Static Binding, so you need PHP 5.3. It can not be done as you want in PHP 5.2. People have tried.

    The workarounds I’ve seen/used for 5.2 are

    1. As you mentioned (and don’t want to do) copy your factory method into each class

    2. Require end-users of the factory to supply the name of the class they’re instantiating as an argument

    3. Require your factories to implement an interface or have them inherit from an abstract class (or just know the convention) that requires the definition of a static “getClassName” method, which will be used in the factory. This is similar to number one, but some people prefer it as it allows their factory method to remain in the “bottom” class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 498k
  • Answers 498k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I have a memory matching game on the market that… May 16, 2026 at 12:13 pm
  • Editorial Team
    Editorial Team added an answer @echo off setlocal enabledelayedexpansion set str=%~1 set str=%str:~7% set str=!str:%%20=… May 16, 2026 at 12:13 pm
  • Editorial Team
    Editorial Team added an answer UIDeviceOrientation is not an actual variable or constant, it's a… May 16, 2026 at 12:13 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a complex value object class that has 1) a number or read-only
I have a class that has purely static Methods and properties. I am calling
(C#, Rhino Mocks, MbUnit). I have a class called AccountManager that has a RegisterUser()
I have a model that has several properties. The properties can be primitive (String)
I have an app that has settings the user can pick before running the
In this Java code: public class Main { public static void main(String[] args) {
I'm currently working on a class that calculates the difference between two objects. I'm
I have written my Container<T> class which backups its T items in several collections
I may be going about this backwards... I have a class which is like
I have a bit of a complex situation. I am building an iPhone app

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.