ravand
یک شنبه 18 اسفند 1392, 11:53 صبح
در مورد متدهای سازنده و تخریب کننده مطالب زیادی خوندم ولی هنوز توش موندم بود و نبود این متدها چه فرقی میکنه!؟
میگن برای ابتدا و انتهای شی بکار میره. برای ساختن و خراب کردن و این حرفا!
الان این برنامه رو ببینید:
<?phpclass obj { public function __construct() { echo "a<br/>"; } public function __destruct() { echo "b<br/>"; }}
$obj = new obj();echo "c<br/>";unset($obj);echo "destructed\n";?>
بود و نبودش چه فرقی میکنه؟
یا حتی این:
<?php// class definitionclass Person { // define properties public $name; public $weight; public $age; public $colour; // constructor public function __construct() { $this->age = 0; $this->weight = 8; $this->colour = "white"; } public function ran($miles){ echo $this->name." ran ".$miles." miles without food or water.<br/>"; } //destructor public function __destruct(){ echo $this->name." still weighed ".$this->weight." units and died."; }}$thomas = new Person;$thomas->name = "Thomas";$thomas->ran(10);$thomas->ran(218);?>
من نمی فهمم درست این متدها توی این برنامه ها چکاری انجام میدن؟ یعنی اگه این متدها باشن یا نباشن چی میشه؟
متشکرم.
میگن برای ابتدا و انتهای شی بکار میره. برای ساختن و خراب کردن و این حرفا!
الان این برنامه رو ببینید:
<?phpclass obj { public function __construct() { echo "a<br/>"; } public function __destruct() { echo "b<br/>"; }}
$obj = new obj();echo "c<br/>";unset($obj);echo "destructed\n";?>
بود و نبودش چه فرقی میکنه؟
یا حتی این:
<?php// class definitionclass Person { // define properties public $name; public $weight; public $age; public $colour; // constructor public function __construct() { $this->age = 0; $this->weight = 8; $this->colour = "white"; } public function ran($miles){ echo $this->name." ran ".$miles." miles without food or water.<br/>"; } //destructor public function __destruct(){ echo $this->name." still weighed ".$this->weight." units and died."; }}$thomas = new Person;$thomas->name = "Thomas";$thomas->ran(10);$thomas->ran(218);?>
من نمی فهمم درست این متدها توی این برنامه ها چکاری انجام میدن؟ یعنی اگه این متدها باشن یا نباشن چی میشه؟
متشکرم.