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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:25:19+00:00 2026-05-10T20:25:19+00:00

Is something like the following possible in PHP? $blah = ‘foo1’; class foo2 extends

  • 0

Is something like the following possible in PHP?

$blah = 'foo1';  class foo2 extends $blah {     //... }  class foo1 {     //... } 

This gives an error.

I want to dynamically set $blah so I can extend whatever class I want.

Edit: The reason for wanting to do this because I wanted to use a function out of another class in a related class. In the end it would have been something like:

Final extends foo1 extends foo2 extends foo3 extends foo4 extends parent { ... } 

In the end I decided to instantiate the other class within the class and use it. Not the best options because they both you 2 of the same classes, but this won’t be used that often, so it will work for now.

  • 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. 2026-05-10T20:25:20+00:00Added an answer on May 10, 2026 at 8:25 pm

    You’re assuming here php executes top to bottom, but it doesn’t quite work like that:

    <?php foo();  # works  function foo(){   print 'bar'; } 

    <?php  foo();  #dies  if( $i == 1 ) {   function foo(){     print 'bar';   } } 

    <?php $i = 1; if( $i == 1 ) {   function foo(){     print 'bar';   } }  foo(); #works 

    Now, although you can conditionally create classes:

    <?php  class A { } class B { } if( false ){    class C extends B {      public static function bar(){       print 'baz';      }   } } C::bar(); # dies 

    You cant instantiate one at runtime from a variable:

    <?php class A { } class B { } $x = 'B';  if( false ){    class C extends $x {      public static function bar(){       print 'baz';      }   } } C::bar(); ---> Parse error: syntax error, unexpected T_VARIABLE, expecting T_STRING in /tmp/eg.php on line 7 

    There is a way to do it with Eval, but you really don’t want to go there:

    <?php  class A { } class B { } $x = 'B';  if( true ){   $code =<<<EOF   class C extends $x {      public static function bar(){       print 'baz';      }   } EOF;    eval( $code ); } C::bar(); $o = new C;  if ( $o instanceof $x ) {   print 'WIN!\n'; } --->barWIN! 

    However, there is a more important question here:

    Why the hell would you want to extend a different class at runtime

    Anybody using your code will want to hold you down and whip you for that.

    ( Alternatively, if you’re into whipping, do that eval trick )

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

Sidebar

Ask A Question

Stats

  • Questions 128k
  • Answers 128k
  • 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 usually do this by appending the time to the… May 12, 2026 at 5:43 am
  • Editorial Team
    Editorial Team added an answer By linking with the right import library. Add advapi32.lib to… May 12, 2026 at 5:43 am
  • Editorial Team
    Editorial Team added an answer Take a look at http://msdn.microsoft.com/en-gb/magazine/dd148646.aspx?pr=blog. It describe five 'ranking' systems.… May 12, 2026 at 5:43 am

Related Questions

Is something like the following possible in PHP? $blah = 'foo1'; class foo2 extends
Here is the updated question: the current query is doing something like: $sql1 =
Why is it not possible to do something equivalent to this in PHP: (Array(0))[0];
A lot of frameworks use URL conventions like /controller/action/{id} which is great, but if
Is it possible to read the data in the php $_SESSION array in the

Trending Tags

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

Top Members

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.