Results 1 to 7 of 7

Thread: public / private static method

  1. #1
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default public / private static method

    so a static method allows you to call the method within the class by instantiating it first?
    Code:
    class Foo
    {
    	public static function dataMatch()
    	{
    		
    	}
    	
    	$match = Match->dataMatch();
    }
    when would you use a static vs non static method?
    Last edited by ggalan; 05-11-2011 at 07:09 PM.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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:
    PHP Code:
    $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:
    PHP Code:
    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).
    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

  3. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    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.

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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

  5. The Following User Says Thank You to djr33 For This Useful Post:

    ggalan (05-08-2011)

  6. #5
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    definitely practical advice. making too many static methods is something of a slippery slope.

  7. The Following User Says Thank You to traq For This Useful Post:

    ggalan (05-08-2011)

  8. #6
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

    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?

  9. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •