Hilmi
Hilmi

Reputation: 3441

how to deal with a class by string in PHP

I have a call lets assume its called A

public class A{
...
}

how can I access the members of this class while I have the name of the class

what I need is something like this

{"A"}::x=5;

instead of

A::x=5;

Upvotes: 0

Views: 31

Answers (2)

deceze
deceze

Reputation: 522109

class Foo {
    const BAR = 'bar';
    public static $baz = 'baz';
}

$foo = 'Foo';
echo $foo::BAR;
echo $foo::$baz;

This requires PHP 5.3+ though.

Upvotes: 5

Andreas Wong
Andreas Wong

Reputation: 60526

You can use ReflectionClass

class A {
        public static $x = 5;
}

$class = new ReflectionClass('A');
echo $class->getStaticPropertyValue('x');

http://php.net/manual/en/class.reflectionclass.php

Upvotes: 4

Related Questions