473,396 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Class_Name::$_static_property

guillermobytes
Hi,
sorry for the title the problem is hard to describe in so few characters.

i need to be able to get a static class property from a dynamic class name:

i have a class name stored as a string:
Expand|Select|Wrap|Line Numbers
  1. $className = 'My_Class_Name';
and i need to call a static property of that class, normally i would do:
Expand|Select|Wrap|Line Numbers
  1. My_Class_Name::$staticProperty;
but the class name changes dynamically, so i cannot hard type it like here up. so i need to do something like this:
Expand|Select|Wrap|Line Numbers
  1. $className::$staticProperty;
is this correct or will it crash?

thanks!
Aug 2 '10 #1
2 1304
dlite922
1,584 Expert 1GB
it will crash. syntax error:

But you can use eval(), like so:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. class foo
  4. {
  5.     public static $bar = 'It Works!'; 
  6. }
  7.  
  8. $my_class = 'foo'; 
  9. $my_static= 'bar';
  10.  
  11. echo eval("return $my_class::\$$my_static;"); 
  12.  
  13.  
The output is:
Expand|Select|Wrap|Line Numbers
  1. It Works!
  2.  
Cheers,


Dan
Aug 3 '10 #2
zorgi
431 Expert 256MB
From PHP Version 5.3 it should not crash and you can do what guillermobytes suggests.

http://php.net/manual/en/language.va...s.variable.php
Aug 3 '10 #3

Sign in to post your reply or Sign up for a free account.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.