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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:31:49+00:00 2026-05-30T16:31:49+00:00

I have a string containing the class name and I wish to get a

  • 0

I have a string containing the class name and I wish to get a constant and call a (static) method from that class.

<?php
$myclass = 'b'; // My class I wish to use

$x = new x($myclass); // Create an instance of x
$response = $x->runMethod(); // Call "runMethod" which calls my desired method

// This is my class I use to access the other classes
class x {
    private $myclass = NULL;

    public function __construct ( $myclass ) {
        if(is_string($myclass)) {
            // Assuming the input has a valid class name
            $this->myclass = $myclass;
        }
    }

    public function runMethod() {
        // Get the selected constant here
        print $this->myclass::CONSTANT;

        // Call the selected method here
        return $this->myclass::method('input string');
    }
}


// These are my class(es) I want to access
abstract class a {
    const CONSTANT = 'this is my constant';

    public static function method ( $str ) {
        return $str;
    }
}

class b extends a {
    const CONSTANT = 'this is my new constant';

    public static function method ( $str ) {
        return 'this is my method, and this is my string: '. $str;
    }
}
?>

As I expected (more or less), using $variable::CONSTANT or $variable::method(); doesn’t work.

Before asking what I have tried; I’ve tried so many things I basically forgot.

What’s the best approach to do this? Thanks in advance.

  • 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-30T16:31:50+00:00Added an answer on May 30, 2026 at 4:31 pm

    To access the constant, use constant():

    constant( $this->myClass.'::CONSTANT' );
    

    Be advised: If you are working with namespaces, you need to specifically add your namespace to the string even if you call constant() from the same namespace!

    For the call, you’ll have to use call_user_func():

    call_user_func( array( $this->myclass, 'method' ) );
    

    However: this is all not very efficient, so you might want to take another look at your object hierarchy design. There might be a better way to achieve the desired result, using inheritance etc.

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

Sidebar

Related Questions

I have a string, containing an Class name. It is, for example, a string
I want to convert a string to a class name. Imagine that I have
I have a string containing the name of a generic class with specified type:
In a Python package, I have a string containing (presumably) the name of a
If I have an f# function that takes a string containing a sentence, what
If I have code like this: string s = MyClass.GetString(); // Returns string containing
From the registry, for a given file type, I get a string containing something
I have a very simple xml string that I loaded using XmlDocument class. Now
We have a class that for containing filter criteria used in searches. In the
I've got simple class public class Person { string Name { get; set; }

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.