Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP get class from stored string of full class path

Tags:

php

I store a PHP Class name with the namespace as a string, in my database. Example: "App\Fields\TextField". Later on, based on that string, I want to read a static variable on the class, that the string defines. How could I do that?

like image 982
Rando Hinn Avatar asked Jan 28 '23 17:01

Rando Hinn


1 Answers

You could actually just reach your variable like this:

namespace Foo;

class Test {

    public static $foo = "BAR";

}

$string = "Foo\Test";

var_dump($string::$foo); // Output: BAR
like image 136
Ole Haugset Avatar answered Feb 19 '23 12:02

Ole Haugset