Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: static keyword

  1. #1
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts

    static keyword

    Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can).

    For compatibility with PHP 4, if no visibility declaration is used, then the property or method will be treated as if it was declared as public.

    Because static methods are callable without an instance of the object created, the pseudo-variable $this is not available inside the method declared as static.

    Static properties cannot be accessed through the object using the arrow operator ->.

    Calling non-static methods statically generates an E_STRICT level warning.

    So now i am learning about the keyword static. I read this on php.net and set about testing some of these statments.

    i make a simple class

    PHP Code:
    class Foo {
        public static function 
    aStaticMethod() {
            
    // ...
            
    echo "hello";
        }
    }

    Foo::aStaticMethod(); 
    I then take away the static keyword from the code

    PHP Code:
    error_reporting(E_ALL);
    class 
    Foo {
        public  function 
    aStaticMethod() {
            
    // ...
            
    echo "hello";
        }
    }

    Foo::aStaticMethod(); 
    and i get exactly the same result. So it seems i can call a non static method statically. Why is this? Have i misunderstood this statement?

    Then i set about testing another statement

    Because static methods are callable without an instance of the object created, the pseudo-variable $this is not available inside the method declared as static.
    PHP Code:
    class Foo {

        static public 
    $property "hello";
        public static function 
    aStaticMethod() {
            
    // ...
            
    echo $this->$property;
        }
        

    }

    Foo::aStaticMethod(); 
    i get
    PHP Fatal error: Using $this when not in object context in /Applications/MAMP/htdocs/zend/static2.php on line 8

    but i can use
    PHP Code:
    class Foo {

        static public 
    $property "hello";
        public static function 
    aStaticMethod() {
            
    // ...
            
    echo self::$property;
        }
        

    }

    Foo::aStaticMethod(); 
    This works. I'm getting somewhere. But why do i have to use the scope resolution operator :: why can't i use the $this-> in here. I thought the whole point was to allow access to a member without having to create an instantiation. So giving you access from outside as with Foo::aStaticMethod() but why does it matter what you use inside the class.

    Im really at a loss here at understanding this whole static concept any help would be greatly appreciated. It seems to me you might as well just use a procedural function instead of a static function.
    Last edited by timgolding; 02-16-2012 at 09:09 PM.
    You can not say you know how to do something, until you can teach it to someone else.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    $this != self::.
    $this refers to a context object. Static refers to context "self" or "class". Any effect in static reflects on every instance of said object, so it cannot be applied at a level of $this.

    PHP is a forgiving language. Issue an error_reporting(E_ALL | E_STRICT);. Strict will indicate you cannot use a static method / variable in a non-static fashion. You can however use a static variable in a non-static method. The third one you have if called in a non-static fashion will complain that the variable is non-static and then issue an error on the read of the member property. This is a great reason why __set should always throw an exception since its the only way to create two variables of the same name (although in different states). $this->property isn't the same as self::$property.

    Static isn't designed to work in the context of an object, its designed to work in the context of a class. It can increment a static variable (same as static in a flat procedural function as well) and have it apply to all classes of that type. You can use it to set or change an iterator type you want to apply to any instance of said collection. Its used to create singletons. I use it to merge two objects of a specific type to create a new object of that type. There are a number of great reasons that you may choose to use static.

    Static *would* be used in conjunction to make final variables as well. PHP did not choose this route though; instead they introduced the const keyword. Java for example would use public static final int MYNUMBER = 1;.

  • Users who have thanked Fou-Lu for this post:

    timgolding (02-17-2012)

  • #3
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    Ok thanks...

    I added the E_STRICT level warnings and saw the error message when trying to access a non static method statically. Thanks.

    So static method work the same as static variables in the procedural approach. So if i change the static property from hello to goodbye statically any new object or static call to that property will give me the new value 'goodbye'

    PHP Code:
    <?php
    error_reporting
    (E_ALL E_STRICT);
    class 
    Foo {

        static public 
    $property "hello"// static properies can not be accessed through the object using ->
        
    public static function aStaticMethod() {
            
    // ... A static methods cant use $this-> because they are not in object context.
            
    echo self::$property;
        }
        
        public function 
    nonStaticMethod() {
            
    // a non static method cant be called statically.
            //Calling non-static methods statically generates an E_STRICT level warning. 
        
    }
    }

    Foo::aStaticMethod(); // outputs hello. called statically with scope resolution operator.
    echo "<br />";
    Foo::$property "goobye"// static property is changed to apply to all classes of that type. 
    Foo::aStaticMethod(); //  outputs goodbye as property is static.

    Foo::nonStaticMethod(); // generates an E_STRICT level warning.


    echo "<br />";
    $foo = new Foo;
    $foo->aStaticMethod(); // static property has been changed for all instances of class

    ?>
    I have made this script as a revision script and commented the key points i need to learn. Is there anything else i should know about static? Some examples of where i might use this might also be helpful.
    Last edited by timgolding; 02-17-2012 at 10:29 AM.
    You can not say you know how to do something, until you can teach it to someone else.


  •  

    Tags for this Thread

    Posting Permissions

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