note 29494 deleted from ref.classobj by didou

From: Date: Mon, 01 Mar 2004 11:52:42 +0000
Subject: note 29494 deleted from ref.classobj by didou
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-66035@lists.php.net to get a copy of this message
Note Submitter: info@free-dev.com ---- 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 ?>

« previous php.notes (#66035) next »