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

  • SEARCH
  • Home
  • 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 1011343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T09:13:10+00:00 2026-05-16T09:13:10+00:00

I was working on an abstract class to save on some code for a

  • 0

I was working on an abstract class to save on some code for a couple of classes. These classes are all factories that instantiate themselves through different static calls. I could save some code by putting all those methods in an abstract class.

However, I ran into a late static binding problem… since our web host isn’t using 5.3 or later, I don’t have access to get_called_class. If I have

$class = __CLASS__;
return new $class();

in the abstract class, __CLASS__ is the name of the abstract class, when I actually want it to use the called class.

I’ve seen examples of abstract factories on the web where the child classes have their own instantiation methods, and don’t rely on the abstract parent for it. However, in this situation, the only reason for the abstract class is to save code, so if I can’t do it there, the value of it diminishes greatly.

Is there a workaround in php < 5.3? debug_backtrace()?


Edit:

I did a test and it seems debug_backtrace() will not work! I guess this is why we need late static binding.

<?

abstract class abstractFactory {
    public function create() {
            print_r(debug_backtrace());
            $class = __CLASS__;
            return new $class();
    }
}

class concreteFactory extends abstractFactory {}

$chimborazo = concreteFactory::create();

and the result:

$ php test.php
Array
(
    [0] => Array
        (
            [file] => /var/www/test.php
            [line] => 13
            [function] => create
            [class] => abstractFactory
            [type] => ::
            [args] => Array
                (
                )

        )

)

Fatal error: Cannot instantiate abstract class abstractFactory in /var/www/test.php on line 7
  • 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-16T09:13:10+00:00Added an answer on May 16, 2026 at 9:13 am

    Once way to do it is to override the various instantiation methods, and pass the name of the class directly:

    <?
    
    abstract class abstractFactory {
    
        public function create($class) {
            return new $class();
        }
    
        public function instantiate($class) {
            return new $class();
        }
    
    }
    
    class concreteFactory extends abstractFactory {
    
        public function create() {
            parent::create(__CLASS__);
        }
    
        public function instantiate() {
            parent::instantiate(__CLASS__);
        }
    }
    
    
    $chimborazo = concreteFactory::create();
    $chimborazo = concreteFactory::instantiate();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working through some sample code and then this appeared: public abstract class RandomPool<T>
I have a repository class that defines some basic Get/Save/Delete methods. Inside these, I
I've been working with some abstract classes and something feels off but I'm not
Working with C#. I have an abstract class that I use to read/write settings
I am working on creating abstract activity classes for my application that I will
In a proof-of-concept project I'm working on, I have a generic abstract class that
I am working on a series of classes that all inherit from a single,
i have got below code which is working fine: public abstract class Beverage {
I have an abstract class that a lot of child classes inherit: public abstract
I am working on a simple abstract database class. In my usage of this

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.