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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:19:57+00:00 2026-06-04T02:19:57+00:00

Arbitrary PHP question, more/less out of curiosity. I apologize if this is a silly

  • 0

Arbitrary PHP question, more/less out of curiosity. I apologize if this is a silly question.

$class  = 'ClassName';
$object = new $class();

or

$object = $class::functionName();

This obviously works.

define(CLASS, 'ClassName');
$object = new CLASS();

or

$object = CLASS::functionName();

This does not work.

Just wondering if it is at all possible to use defined constants to create new objects. If not, I definitely understand why.

  • 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-04T02:19:59+00:00Added an answer on June 4, 2026 at 2:19 am

    Well, you can do this:

    define(MYCLASS, 'ClassName');
    $class = MYCLASS;
    $object = new $class();
    

    …but as far as I know, you can’t use the constant directly as in new MYCLASS(), because it will of course look for the class named MYCLASS.

    Side note: You can’t have constants with names that conflict with reserved words, like class or function, even if they are uppercased.

    You could do something silly like this if for some reason you’re doing this a lot:

    function newClass($str, $args = NULL) {
        return new $str($args);
    }
    $class = newClass(MYCLASS);
    

    …but I don’t really recommend it. I’d also guess that you probably don’t need so many constants, or there’s a more elegant way to do what you’re doing that doesn’t involve constants at all.

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

Sidebar

Related Questions

In line with this question: Lightweight web app server for PHP? The above question
I'm currently trying to figure out how to work with arbitrary-precision numbers in PHP.
Given some arbitrary url: view.php?viewid=blahblahblah Is there a way I can encode a query
This is really a two part question, since I don't fully understand how these
I was like on every question about this on the net, tried like 50
How can I pass a arbitrary number of arguments to the class constructor using
I have a list similar to this (with an arbitrary amount of li's): <ul>
I was reading this tutorial for a simple PHP login system . In the
I have written a php script that returns an arbitrary number of specific ids
I have this sphinx search engine that I use through Zend using sphinxapi.php .

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.