Translated by Google Translate

Constructor and Destructor in OOP (PHP)



In OOP, you will never separate from constructor and destructor. What is constructor and destructor in OOP? Constructor is special statement (also called special method) that created when an object created (instance of class). If you didn't create the constructor or destructor in a class then constructor and destructor automatically created. When you create $objPeople in the previous OOP tutorial, then constructor and destructor automatically created

Normally, the constructor is created to provide an initial operation to be performed when an object created (inilization object). This is the writting format how to create a constructor

function __constructor(parameter) {
    code
}

Keyword construct should started with double underscore and then give the parentheses (). Among the parentheses, you can give the parameter if you think you need to add parameter. Then you can build your code in constructor.

Eg you create class product, in that class include property of quantity that the initialize value shuld be 0, then the code will look like below :
class barang {
    private name;
    private price;
    private quantity;

    function __construct() {
        $this->quantity = 0;
    }
}

If the constructor created and used when the object created, then destructor is the reverse of constructor. Destructor created and used when the object deleted. In PHP programming, destructor automatically called in your final PHP scripting.

Same like constructor, writing format of destructor begin by double underscore. Here it is :

function __destructor(parameter) {
    code
}

And this another example of constroctor and destructor :

<?php
class people {
    private $name;

    function __construct($name) {
        $this->name=$name;
        echo "Constructor : $this->name created <br>";
    }


    function speaking() {
        echo "Hallo, my name is ".$this->name." <br />";
    }

    function __destruct() {
        echo "Destructor : $this->name deleted <br/ >";
    }

}

$people1 = new People("People 1");
$people1->speaking();
$people2 = new People("People 2");
$people2->speaking();
?>

And this is the result in browser :


In the picture above, there are six teks, the result from calling the constructor, calling method speaking(), and calling destructor. Though you don't put code call method __construct() & method __destruct(). This happen because when you create an object, it's automatically creating constructor for the object. Then when it has used, it will removed from memory. It's automatically the destructor from that object will be called.

Here is the example of the use constructor, destructor, method, object, and property for calculating the student's grade.


<?php
class Score {
    private $task;
    private $middleTest;
    private $finalTest;
    // constructor set first score with 0
    function __construct() {
        $this->task=0;
        $this->middleTest=0;
        $this->finalTest=0;
        echo “Constructor : Task, Middle Test and Final Test Score set to 0.<br /><br />”;
    }
    //function to set task score, set from 0 until 100
    function settask($score) {
        if (($score<=100) && ($score>=0))
            $this->task=$score;
    }
    //function to set value of middle test, set from 0 until 100
    function setmiddleTest($score) {
        if (($score<=100) && ($score>=0))
            $this->middleTest=$score;
    }
    //function to set final test score, set from 0 until 100
    function setfinalTest($score) {
        if (($score<=100) && ($score>=0))
            $this->finalTest=$score;
    }
    //function to get value of task property
    function gettask() {
        return $this->task;
    }
    //function to get value of middle test property
    function getmiddleTest() {
        return $this->middleTest;
    }
    //function to get value of final test property
    function getfinalTest() {
        return $this->finalTest;
    }
    //function to count the last scores
    function getLS() {
        $lastscores=0.2*$this->task+0.3*$this->middleTest+0.5*$this->finalTest;
        return $lastscores;
    }
    //function to view value of task, middle test final test and last scores
    function view() {
        echo    “Task Score : “.$this->task.
            “, Middle Test Score : “.$this->middleTest.
            “, Final Test Score : ”.$this->finaTest.
            “, Last Scores : ”.$this->getLS().“<br />”;
    }
    //destructor to deleting object from memory
    Function __destruct() {
        echo    “<br /><br />Destructor : Task Score,
            Middle Test and Final Test deleted from Memory<br />”;
    }
}
// e.g implementing class Score
$score = new Score();
$score->settask(80); //set task score
echo “Latest Task Score : “.$score->gettask().”<br />”;
$score->setmiddleTest(60); //set middle test score
$score->setfinalTest(80); //set final test score
$score->view();
// final test score has not change, new score is not valid (> 100)
$score->setfinalTest(110);
$score->view();
echo “Last Scores : ”.$score->getLS();
?>

And the looks like in browser is :





 

©2013 @namakuherman | Developed by Herman Creative Industry