note 79381 deleted from language.oop5.basic by felipe

From: Date: Mon, 24 Mar 2008 22:13:36 +0000
Subject: note 79381 deleted from language.oop5.basic by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-137477@lists.php.net to get a copy of this message
Note Submitter: SID TRIVEDI ---- <?php /* The script below illustrate the use of $this pseudo variable and Scope Resolution Operator :: Hope, it will be useful for beginners learning PHP OOPs, coz it contains Basic Class defination Inheritance / Extension of a basic Class Use of pseudo variable $this and Scope Resolution Operator :: */ class SimpleClass{ // member declaration public $var = 'The default value must be a constant expression, not (for example) a variable, a class member or a function call.'; // method declaration public function displayVar() { echo '$this - a pseudo-variable is available refering to the calling object under operation'; echo "\n<br>"; echo $this->var; //$this referes to the calling object under operation. echo "\n<br>"; } } class ExtendedClass extends SimpleClass{ // Redefine - Extending the parent SimpleClass. function displayVar(){ $var = 'Extending SimpleClass by Inheritance. This is result of overriding function displayVar in ExtendedClass.'; echo "$var.\n<br><br>"; echo "The Scope Resolution Operator :: allows access to static, constant, and overridden members or methods of a class.\n<br>"; // SimpleClass::displayVar(); // It's always safe to use name of the class. parent::displayVar(); // The SRO :: allows access to static, constant, and overridden members of a parent class. echo "\n<br>"; } } $obj = new SimpleClass(); // Object creation and assignment for a parent SimpleClass. $obj->displayVar(); // Object is being passed to the function call in a SimpleClass. $extended_obj = new ExtendedClass(); // Object creation and assignment for a child ExtendedClass. $extended_obj->displayVar(); // Object is being passed to the function call in an ExtendedClass. /* Above codes produces following output: $this - a pseudo-variable is available refering to the calling object under operation The default value must be a constant expression, not (for example) a variable, a class member or a function call. Extending SimpleClass by Inheritance. This is result of overriding function displayVar in ExtendedClass.. The Scope Resolution Operator :: allows access to static, constant, and overridden members or methods of a class. $this - a pseudo-variable is available refering to the calling object under operation The default value must be a constant expression, not (for example) a variable, a class member or a function call. */ ?>

« previous php.notes (#137477) next »