note 35713 added to language.oop

From: Date: Thu, 11 Sep 2003 21:15:03 +0000
Subject: note 35713 added to language.oop
Groups: php.notes 
Request: Send a blank email to php-notes+get-56276@lists.php.net to get a copy of this message
In response to the post from 'jim at thestapletons dot org' and anyone else with this confusion: As it says in the documentation: "In PHP 4, only constant initializers for var variables are allowed. To initialize variables with non-constant values, you need an initialization function which is called automatically when an object is being constructed from the class. Such a function is called a constructor (see below)." So in order for $this->f->blah() to work in class bar you need to initialize $f in the constructor. In addition you can't use echo "$a"; in foo::blah() unless you declare $a within the blah() method, instead you must use: echo $this->a; to refer to the class variable $a. Variables declared in the class but outside a method are local to the class and variables declared within a method without using the '$this' keyword are local to the method. Here's a full example: <?php class foo { var $a = "blah"; function blah() { echo "$this->a"; } } class bar { var $f; function bar() { $this->f = new foo(); } function fooBlah() { $this->f->blah(); } } $test = new bar(); $test->fooBlah(); ?> ---- Manual Page -- http://www.php.net/manual/en/language.oop.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+35713 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+35713&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+35713&report=yes

« previous php.notes (#56276) next »