TiloW Posted July 12, 2009 Share Posted July 12, 2009 Hey guys =) Here's my problem: // exemplary class class Test { static public DoSomething() {} } // reference of class "Test" $testClassName = "Test"; // create new instance of class "Test" $testInstance = new $testClassName(); // works just fine // call static method of class "Test" $testClassName::DoSomething(); // doesn't work So.. How can i call a static method of a class which is strored in a variable? Thanks for any help =) Quote Link to comment https://forums.phpfreaks.com/topic/165696-solved-variable-as-classname/ Share on other sites More sharing options...
.josh Posted July 12, 2009 Share Posted July 12, 2009 // reference of class "Test" $testClassName = "Test"; $testClassName is not an object. It's a variable, and you're assigning a regular string to it. Quote Link to comment https://forums.phpfreaks.com/topic/165696-solved-variable-as-classname/#findComment-874054 Share on other sites More sharing options...
TiloW Posted July 12, 2009 Author Share Posted July 12, 2009 true.. but in some situations this string is interpreted as a classname - i've provided one example above. Is there any other way to store classes in variables? Why does the instantiation work, but not the call of a static method?! Quote Link to comment https://forums.phpfreaks.com/topic/165696-solved-variable-as-classname/#findComment-874056 Share on other sites More sharing options...
.josh Posted July 12, 2009 Share Posted July 12, 2009 That's not an example of of a string being interpreted as a class name. That's an example of storing a string of value "Test" to a variable $testClassName. If you want to reference a static method like that, you can do Test::doSomething(); or else like $testClassName = "Test"; eval($testClassName."::doSomething();"); Quote Link to comment https://forums.phpfreaks.com/topic/165696-solved-variable-as-classname/#findComment-874061 Share on other sites More sharing options...
ignace Posted July 12, 2009 Share Posted July 12, 2009 You may want to take a look at the factory method design pattern: http://en.wikipedia.org/wiki/Factory_method_pattern Quote Link to comment https://forums.phpfreaks.com/topic/165696-solved-variable-as-classname/#findComment-874064 Share on other sites More sharing options...
TiloW Posted July 12, 2009 Author Share Posted July 12, 2009 Well.. I don't really get the difference because in $testInstance = new $testClassName(); the value of $testClassName is interpreted as a name of a class ..isn't it? Anyway.. the eval function is exactly what i was looking for! thanks Crayon =) Quote Link to comment https://forums.phpfreaks.com/topic/165696-solved-variable-as-classname/#findComment-874065 Share on other sites More sharing options...
.josh Posted July 12, 2009 Share Posted July 12, 2009 Well.. I don't really get the difference because in $testInstance = new $testClassName(); the value of $testClassName is interpreted as a name of a class ..isn't it? No. Class names (and functions) are not parsed inside quotes like that. Only variables arrays and objects. Quote Link to comment https://forums.phpfreaks.com/topic/165696-solved-variable-as-classname/#findComment-874074 Share on other sites More sharing options...
TiloW Posted July 12, 2009 Author Share Posted July 12, 2009 but why does it work? ^^ *confused Quote Link to comment https://forums.phpfreaks.com/topic/165696-solved-variable-as-classname/#findComment-874134 Share on other sites More sharing options...
.josh Posted July 12, 2009 Share Posted July 12, 2009 Why does what work...this? Well.. I don't really get the difference because in $testInstance = new $testClassName(); the value of $testClassName is interpreted as a name of a class ..isn't it? edit: Accidentally hit post. It does work, because it will create an instance of the object using the value of that variable. But that is not the same as linking that variable to the class, or somehow making an object out of a variable. You are simply making use of the value. Quote Link to comment https://forums.phpfreaks.com/topic/165696-solved-variable-as-classname/#findComment-874156 Share on other sites More sharing options...
Daniel0 Posted July 12, 2009 Share Posted July 12, 2009 It works in PHP 5.3. Edit: The static calling with a variable as class name (to prevent ambiguity). Quote Link to comment https://forums.phpfreaks.com/topic/165696-solved-variable-as-classname/#findComment-874167 Share on other sites More sharing options...
.josh Posted July 12, 2009 Share Posted July 12, 2009 yeah i just edited my post. I meant to say it doesn't work in the context of it's not the same thing as what he's trying to do as far as using a plain old var as an object. Quote Link to comment https://forums.phpfreaks.com/topic/165696-solved-variable-as-classname/#findComment-874170 Share on other sites More sharing options...
TiloW Posted July 12, 2009 Author Share Posted July 12, 2009 okay.. thanks guys Quote Link to comment https://forums.phpfreaks.com/topic/165696-solved-variable-as-classname/#findComment-874171 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.