note 29494 added to ref.classobj

From: Date: Sat, 15 Feb 2003 08:03:02 +0000
Subject: note 29494 added to ref.classobj
Groups: php.notes 
Request: Send a blank email to php-notes+get-43964@lists.php.net to get a copy of this message
A constructor (a code with runs in the initialization of the class) can be used, like in the C language. Here's an example : <? class int { function int() { echo "constructor"; } } $myint = new int; // creates a new instance and call the constructor ?> The constructor's function name MUST be the same as the class name. If you want to pass arguments in the constructor, use this : <? class int { function int($str) { echo $str; } } $myint = new int("hello"); ?> Now you can show hello at the initialization of the class. I tried to see if I could use the destructor (~classname) but it doesn't seems do work :-( it you want to use a variable in a class, you must create a new instance of it. Example : <? class i { var $m=array("a", "b", "c"); function s() { echo $m[0]; } } i::s(); // it doesn't work because $m is set to nothing $int = new i; // create instance and sets m $int->s(); // now it shows the right value ?> -- http://www.php.net/manual/en/ref.classobj.php http://master.php.net/manage/user-notes.php?action=edit+29494 http://master.php.net/manage/user-notes.php?action=delete+29494 http://master.php.net/manage/user-notes.php?action=reject+29494

« previous php.notes (#43964) next »