so a static method allows you to call the method within the class by instantiating it first?
when would you use a static vs non static method?Code:class Foo { public static function dataMatch() { } $match = Match->dataMatch(); }
so a static method allows you to call the method within the class by instantiating it first?
when would you use a static vs non static method?Code:class Foo { public static function dataMatch() { } $match = Match->dataMatch(); }
Last edited by ggalan; 05-11-2011 at 07:09 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:
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:PHP Code:$db = new dbclass;
$db->method();
//....
//but in some cases:
if (dbclass::dbserverisonline()) {
echo 'DB Server is Online!';
}
(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.)PHP Code:preg_replace_callback($pattern,"myclass::mymethod",$text);
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).
Last edited by djr33; 05-08-2011 at 05:30 PM.
Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
I use static methods when I might need to execute them in many different places in a script (e.g., something likeuser::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.
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.
Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
ggalan (05-08-2011)
ggalan (05-08-2011)
would this be the same as private and public function then?static methods/properties can be accessed using the following syntax, from OUTSIDE an object:
class::method();
Non-static methods/properties cannot.
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
Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
Bookmarks