mentalist Posted May 18, 2011 Share Posted May 18, 2011 Hey, I've not used PHP for a while and never really used classes! I was reading some code today and I saw a statement something like: $s = &MyClass:"Something"); From reading the PHP Manual it has an example which looks like this for 5.3, however my server still uses 5.2 (.9), how do I do similar using that? Preferably something along these lines... class spook{ function __construct($n){ if($n==1){ return "hello"; } else { return "goodbye"; } } } $a=spook:); print $a; Cheers! i.e. What was the old method before the Scope resolution operator? Quote Link to comment https://forums.phpfreaks.com/topic/236790-class-return/ Share on other sites More sharing options...
KevinM1 Posted May 18, 2011 Share Posted May 18, 2011 The code you saw was invoking a static method. It also looks like old, PHP 4 code as it returns a reference (the '&' symbol), which is very rarely used in PHP. http://php.net/manual/en/language.oop5.static.php You can't do something like: $obj = class:); Because it makes no sense, logically. A constructor creates a new, singular object of its class. Static denotes class functionality that is invoked without an object. Quote Link to comment https://forums.phpfreaks.com/topic/236790-class-return/#findComment-1217250 Share on other sites More sharing options...
mentalist Posted May 18, 2011 Author Share Posted May 18, 2011 That's good enough for me, many thanks... class foo { public static function test($n) { if($n==1){ return "hello"; } else { return "goodbye"; } } } $a = foo::test(1); print $a; It's just I thought I may do away with so many globals for simple things, but for some things i'm not sure if the overhead will be worth it... Do you know how this works, say if I called it a second time, or another function within the class, would the class already be defined in memory or would it have already been destroyed and need to be initialised again? Quote Link to comment https://forums.phpfreaks.com/topic/236790-class-return/#findComment-1217255 Share on other sites More sharing options...
pornophobic Posted May 18, 2011 Share Posted May 18, 2011 Using objects would be easier and less hassle if you have multiple methods in a class. class foo { public static function test($n) { if($n==1){ return "hello"; } else { return "goodbye"; } } //note that the "static keyword isn't always needed, it's the default for methods create din php function best($m) { if ($m=2) { return "mm"; } else { return "bb"; } } } $baz = new foo(); $a = $baz->test(1); //this will work along with the scope op. $c = $baz->best(4); //this will only work when called from the object, $baz. // Won't work, # $d = foo::best(4); echo $a; echo '<br />'; echo $c; If you give $a the result of foo::test(1), $a will stay that value until it is replaced with something else or is unset[/code] in your script. Quote Link to comment https://forums.phpfreaks.com/topic/236790-class-return/#findComment-1217298 Share on other sites More sharing options...
mentalist Posted May 18, 2011 Author Share Posted May 18, 2011 Sorry I should have specified the scope, basically all over... Also i'm not concerned about $a it was the memory usage of the class instantiation and how an unassigned class works. Cheers anyway. Quote Link to comment https://forums.phpfreaks.com/topic/236790-class-return/#findComment-1217308 Share on other sites More sharing options...
KevinM1 Posted May 19, 2011 Share Posted May 19, 2011 It's just I thought I may do away with so many globals for simple things, but for some things i'm not sure if the overhead will be worth it... Do you know how this works, say if I called it a second time, or another function within the class, would the class already be defined in memory or would it have already been destroyed and need to be initialised again? I believe it would still be in memory, but I'm not 100% certain. Static methods should only be used if you don't need to retain state. They're used pretty frequently with the various factory patterns. A common example would be something like: $user = UserFactory::findById(549); UserFactory::findById() would accept an integer id as a parameter and return a User object. The method is static because the factory itself does not have to retain any data after a User is created. It simply takes raw material (an integer id number) and returns a useful product (a User object). Just like a factory in real life. Quote Link to comment https://forums.phpfreaks.com/topic/236790-class-return/#findComment-1217517 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.