Log in

View Full Version : Resolved public / private static method



ggalan
05-08-2011, 04:24 PM
so a static method allows you to call the method within the class by instantiating it first?


class Foo
{
public static function dataMatch()
{

}

$match = Match->dataMatch();
}


when would you use a static vs non static method?

djr33
05-08-2011, 05:24 PM
static methods/properties can be accessed using the following syntax, from OUTSIDE an object:
class::method();

Non-static methods/properties cannot.

http://php.net/manual/en/language.oop5.static.php


Generally for OOP you would not use static methods. But if you need to mix OOP with other styles, you may want to.

For example, you might have a database connection class:

$db = new dbclass;
$db->method();
//....

//but in some cases:
if (dbclass::dbserverisonline()) {
echo 'DB Server is Online!';
}


It is rarely required that you use static methods. However, one example where it is required is if you want to access the contents of a class when you can't create an object. This is true when you are using a callback function:

preg_replace_callback($pattern,"myclass::mymethod",$text);
(It is possible to work around this indirectly by creating an anonymous function and creating a class in that within which you can call the method, but that is more complicated.)


In short, using a static method allows you to borrow a method from a class as a function like any other function you create. Note that if you do use a method statically, the pseudo-variable $this will not be available (because that refers to a class).

traq
05-08-2011, 05:35 PM
I use static methods when I might need to execute them in many different places in a script (e.g., something like user::getUserName() which simply retrieves a logged-in user's name from the $_SESSION), but I don't need the overhead of building the class or running any of the internal, interdependent functions that might be in the class (such as setting up a DB connection and actually retrieving the user's info, validating that the user is logged in, etc.).

the most basic way to explain it is that static methods are "utility" methods: those that "belong" with their class, but could survive just fine as independent functions and will often need to be accessed from outside the class (as Daniel mentions). If your methods depend on many other functions in the class, and work together with them to achieve the main function of the class, then they should probably not be static.

If you have a specific bit of code you'd like further advice with, post it and we can offer suggestions.

djr33
05-08-2011, 05:38 PM
I think the clearest answer may be this: don't use static methods unless you realize that you must use them-- you'll know because you will want to access a method without using the class.

traq
05-08-2011, 05:45 PM
definitely (http://d-e-f-i-n-i-t-e-l-y.com/) practical advice. making too many static methods is something of a slippery slope.

ggalan
05-10-2011, 09:23 PM
static methods/properties can be accessed using the following syntax, from OUTSIDE an object:
class::method();

Non-static methods/properties cannot.


would this be the same as private and public function then?

djr33
05-10-2011, 10:13 PM
No. Static methods are available WITHOUT an object. Non-static methods must be used as part of an object.

Public methods can be accessed like this:
$myobject->myfunction(); //anywhere
Private methods MUST be accessed within the class definition, not like above. So this usually is done using $this:
$this->myfunction(); //within the class definition