note 35713 deleted from language.oop by didou

From: Date: Fri, 03 Oct 2003 16:35:01 +0000
Subject: note 35713 deleted from language.oop by didou
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-57754@lists.php.net to get a copy of this message
Note Submitter: sethcw@NOSPAMpacbell.net ---- 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(); ?>

« previous php.notes (#57754) next »