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

Related Questions

Is something like the following possible in Python? >>> vars = {'a': 5} >>>
anybody knows if its possible to retrieve some error info (like getsockopt SO_ERROR in
Let's say I have a table that's like the following: name VARCHAR(50) PRIMARY KEY
For the following project I will be using PHP and jQuery. I have the
I have the following situation, I have an html file with image tags about
Background The idea is this: Person provides contact information for online book purchase Book,
I would like to load HTML using c#'s webbrowser component. I looked up MSDN,
I have a declarative base class Entity which defines the column name as polymorphic,
i have have a form in site A and want to submit the form
So today I've been working with RoR for the first time using the book

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.