0

I'm trying to access a static property from a dynamic class.

class A {
    public static $myvar = 'A Class';
}

class B {
    public static $myvar = 'B Class';
}

Somewhere else:

public function getMyVar($classname) {
    return ??????::$myvar; // help here!!
}

$a = getMyVar('A'); // I want 'A Class'
$b = getMyVar('B'); // I want 'B Class'

My Question: how do I access $myvar depending on what gets sent in to getMyVar() ?

EDIT: The class can be one of many (say 50-100 possible) so I'm looking for a short method

Is my only option having to make $myvar not-static??

3 Answers 3

2

I solved it.

The static variable can be accessed by:

public function getMyVar($classname) {
    return $classname::$myvar;
}

That was a lot easier than anticipated! :)

Sign up to request clarification or add additional context in comments.

Comments

0

Something like this?

    function getMyVar($classname) {
            switch($classname) {
                    case 'A': return A::$myvar;
                    case 'B': return B::$myvar;
            }
    }

EDIT: If you have so many classes, maybe you can try with this?

    function getMyVar($classname) {
            eval('$var='.$classname.'::$myvar;');
            return $var;
    }

2 Comments

Fair enough, unfortunately I have many possible classes (50+).
eval is a really bad thing to use
0

This is similar to a previous answer, but performs some sanity checking to ensure that what you're trying to access exists, and is a valid object reference.

Save to demo.php, chmod +x demo.php && ./demo.php.


    #!/usr/bin/php
    <?php

    class A {
      public static $myvar = 'A Class';
      public static $test1 = 'TEST_VALUE_1';
      public static $test2 = 'TEST_VALUE_2';
    }

    class B {
      public static $myvar = 'B Class';
    }


    /**
     * Gets a static variable from the specified class. 
     * Returns FALSE if the property or class does not exist, otherwise returns the value of the property.
     * @param string|object $className Can be either the class name (string) or an object (i.e., "A" or A).
     * @param string $varName
     * @return mixed|boolean 
     */
    function getClassStaticVar($className, $varName) {
      if ((is_string($className) && class_exists($className) ) || is_object($className))
        if (property_exists($className, $varName)) {
          return $className::$$varName;
        }
      return FALSE;  //if your property value is potentially FALSE, you might want to return another value here, like NULL.
    }

    function getMyVar($className) {
      if (is_string($className) || is_object($className)) //is className a string or object reference? 
        return getClassStaticVar($className, 'myvar'); // --or--  return $className::$myvar;
      return FALSE;
    }


    $a = getMyVar('A'); // I want 'A Class'
    $b = getMyVar('B'); // I want 'B Class'

    //in real code, you'd check if $a/$b === FALSE here. (see comment in getClassStaticVar() above)
    echo ($a===FALSE ? 'Property/Class not found' : $a) . PHP_EOL;
    echo $b . PHP_EOL;
    echo getClassStaticVar("A", 'test1') . PHP_EOL;
    echo getClassStaticVar(new A(), 'test2') . PHP_EOL;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.