RE: [PHP] Understanding Static Methods
| From: | Sylvain Girard | Date: | Fri, 19 Nov 2004 02:16:14 +0000 |
| Subject: | RE: [PHP] Understanding Static Methods | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-202475@lists.php.net to get a copy of this message | ||
I'm taking a wild guess here, but doesn't that got anything to do with the
public declaration of the method?
-----Oorspronkelijk bericht-----
Van: Jordi Canals [mailto:jcanals@gmail.com]
Verzonden: vrijdag 19 november 2004 2:29
Aan: PHP List
Onderwerp: [PHP] Understanding Static Methods
Hi all,
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."
Test code:
========
<?php
class B
{
private $str;
public final function __construct()
{
$this->str = 'Hello world';
}
public function showB()
{
echo $this->str, '<br>';
echo 'Showing B<br>';
$this->TestStatic();
}
public static function TestStatic()
{
echo '<br>Inside Static<br>';
}
}
echo error_reporting() . '<br>';
$test = new B;
$test->TestStatic();
$test->showB();
echo "-- END --";
?>
Output:
=====
4095
Inside Static --> Called by $test->TestStatic()
Hello world
Showing B
Inside Static --> Called from $test->showB with
$this->TestStatic()
-- END --
Comments:
========
I'm running PHP 5.0.2 and error_reporting = E_ALL | E_STRICT. As
reported in the output, the values for error_reporting are well set
(4095 == E_ALL | E_STRICT)
I cannot understand why I can access the method TestStatic() with a
variable that is an instance of the class. The manual say I cannot do
it. As you can see, I call the static method with $this->TestStatic()
inside the showB() method; also, I can access it by using the instance
of this class: test->TestStatic().
I get no errors or notices about I'm accessing an static method by
using and instance var. As you can read in the manual, you cannot do
that .... but it works without error.
I'm sure I'm missing something ...
Thanks in advance for comments,
Jordi.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php