Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/236790-class-return/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/236790-class-return/#findComment-1217250
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/236790-class-return/#findComment-1217255
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/236790-class-return/#findComment-1217298
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/236790-class-return/#findComment-1217517
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.