note 42955 added to language.variables.scope

From: Date: Fri, 04 Jun 2004 15:43:31 +0000
Subject: note 42955 added to language.variables.scope
Groups: php.notes 
Request: Send a blank email to php-notes+get-70749@lists.php.net to get a copy of this message
Static variables do not hold through inheritance. Let class A have a function Z with a static variable. Let class B extend class A in which function Z is not overwritten. Two static variables will be created, one for class A and one for class B. Look at this example: <?php class A { function Z() { static $count = 0; printf("%s: %d\n", get_class($this), ++$count); } } class B extends A {} $a = new A(); $b = new B(); $a->Z(); $a->Z(); $b->Z(); $a->Z(); ?> This code returns: A: 1 A: 2 B: 1 A: 3 As you can see, class A and B are using different static variables even though the same function was being used. ---- Manual Page -- http://www.php.net/manual/en/language.variables.scope.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+42955 Delete -- http://master.php.net/manage/user-notes.php?action=delete+42955&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+42955&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#70749) next »