Re: Understanding Static Methods

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



* Jordi Canals <jcanals@xxxxxxxxx>:
> I'm trying to understand static methods in a test class. Reading the
> manual, it says: "A member or method declared with static can not be
> accessed with a variable that is an instance of the object and cannot
> be re-defined in an extending class."

It's possible the wording in the manual is incorrect or misleading. I've
always understood static methods (by definition and experience) to be
methods that do not and cannot make reference to object properties or
methods, and hence cannot affect the overall state of an object instance
or depend on the state of an object instance. In other words, the
following is 'illegal' (though it worked in PHP4):

    static function testStatic()
    {
        $value = $this->Property;
        echo "$value<br />";
    }

but you CAN write:

    static function testStatic($param)
    {
        // Determine the value of $value based on parameters passed
        $value = $param['value'];
        echo "$value<br />";
    }

Either way, you can always reference a static method from elsewhere in
the object or from an object instance:

    public function doSomething()
    {
        $this->testStatic();
        // do other things
    }

or

    $objectInstance->testStatic();

I've been using static methods in exactly this way since 4.2 versions of
PHP4 without issue, and see the same behaviour so far in PHP5. Somebody
please correct me if this is not how they work.

-- 
Matthew Weier O'Phinney           | mailto:matthew@xxxxxxxxxx
Webmaster and IT Specialist       | http://www.garden.org
National Gardening Association    | http://www.kidsgardening.com
802-863-5251 x156                 | http://nationalgardenmonth.org

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux