tdpaxton Posted April 30, 2008 Share Posted April 30, 2008 I'm having some trouble instantiating a class and then calling an inline method. <?php class SomeClass { public function activate($init = false) { return $init; } } if (new SomeClass().activate(true) == true) echo "It works!"; ?> ...gives me the error... Fatal error: Call to undefined function activate() in C:\wamp\www\index.php on line 10 Any suggestions? Thanks! Link to comment https://forums.phpfreaks.com/topic/103593-class-and-inline-method/ Share on other sites More sharing options...
Aeglos Posted April 30, 2008 Share Posted April 30, 2008 This is PHP, not Java To access objects properties and methods you use the -> operator. The dot . operator is used to concatenate values. try: if (new SomeClass()->activate(true) == true) echo "It works!"; Can't remember if you can call a method at the same time you instatiate an object though, might give an error. You can also call the function statically without instatiting the object with the :: operator, as in: if (SomeClass::activate(true) == true) echo "It works!"; Link to comment https://forums.phpfreaks.com/topic/103593-class-and-inline-method/#findComment-530484 Share on other sites More sharing options...
tdpaxton Posted April 30, 2008 Author Share Posted April 30, 2008 Oops, my bad! I deal with about 4 different languages on a regular basis, so I guess its easy to get confused. I tried... if (new SomeClass()->activate(true) == true) echo "It works!"; But I get the error... Parse error: syntax error, unexpected T_OBJECT_OPERATOR in C:\wamp\www\index.php on line 10 Firstly, is it possible to call a method on a newly instantiated object? I realize that I could create the object and then call the method, but I was looking for a cleaner solution. Thanks! Link to comment https://forums.phpfreaks.com/topic/103593-class-and-inline-method/#findComment-530486 Share on other sites More sharing options...
Aeglos Posted April 30, 2008 Share Posted April 30, 2008 I thought it might give an error, and it did... so I guess it's not possible. You can call it statically if it's just a simple generic true/false function, it's the closest to what you wanted. Or simply instatiate it properly beforehand. Link to comment https://forums.phpfreaks.com/topic/103593-class-and-inline-method/#findComment-530489 Share on other sites More sharing options...
blackcell Posted April 30, 2008 Share Posted April 30, 2008 I'm not too much on classes but maybe: <?php $myNew = new SomeClass(); if($myNew -> activate(true) == true)) echo "It works!"; ?> Like I said I have no idea but it seems classes work something like this. Link to comment https://forums.phpfreaks.com/topic/103593-class-and-inline-method/#findComment-530497 Share on other sites More sharing options...
tdpaxton Posted April 30, 2008 Author Share Posted April 30, 2008 This method does work, I was justing looking for something a little cleaner. Thanks to everyone for their help though! Link to comment https://forums.phpfreaks.com/topic/103593-class-and-inline-method/#findComment-530500 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.