Ecommerce Shopify WordPress Discussion

Php: Use a variable value to check class exists with ::class [closed]

Closed. This question needs debugging details. It is not currently accepting answers. Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question. Closed 6 years ago. Improve this question I have the following: if (class_exists($name::class)) { return new $name(); } The class $name will be part of another namespace and this is in a factory method. Php does not like this and throws error: Fatal error: Dynamic class names are not allowed in compile-time How can i get around this?
I remember that some time ago (maybe a week) I heard somewhere on the web, in a presentation about MVC or design patterns (I think), that such a construct as "::class" is not yet implemented in PHP. But maybe in PHP > 7. I'm not sure. Anyway: you don't need to use "::class". But you need to pass a fully qualified name (FQN) to the class_exists. So it has to be an FQN-string. Example: $classFQN = "\myNamespaceName\mySubNamespaceName1\mySubNamespaceName2\myClassName"; if (class_exists($classFQN)) { return new $classFQN(); } Notice the first "\" (back-slash). It makes a namespaced class name to a FULLY qualified name. See "PSR-4: Autoloader" at http://www.php-fig.org/psr/psr-4/

February 13, 2024

function getClassInGlobal($name){ if(isset($GLOBALS[$name])){ if(is_object($GLOBALS[$name])){ $obj = $GLOBALS[$name]; return new $obj(); }else{ return false; }; }else{ return false; }; }; $Files = getClassInGlobal('myFilesClass'); if($Files){ echo '<pre>'; print_r(get_class_methods($Files)); echo '</pre>'; };

February 13, 2024

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.