note 79381 added to language.oop5.basic

From: Date: Sat, 24 Nov 2007 00:35:29 +0000
Subject: note 79381 added to language.oop5.basic
Groups: php.notes 
Request: Send a blank email to php-notes+get-132519@lists.php.net to get a copy of this message
<?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. */ ?> ---- Server IP: 69.147.83.197 Probable Submitter: 208.72.123.66 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.basic.php Edit -- https://master.php.net/note/edit/79381 Del: integrated -- https://master.php.net/note/delete/79381/integrated Del: useless -- https://master.php.net/note/delete/79381/useless Del: bad code -- https://master.php.net/note/delete/79381/bad+code Del: spam -- https://master.php.net/note/delete/79381/spam Del: non-english -- https://master.php.net/note/delete/79381/non-english Del: in docs -- https://master.php.net/note/delete/79381/in+docs Del: other reasons-- https://master.php.net/note/delete/79381 Reject -- https://master.php.net/note/reject/79381 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#132519) next »