-4

Can you give me an example that consist the function __construct and how that code would be if we convert it into procedural php? Maybe this is a kind of dummy question for you, but for me who never learning php oop before really got confused when i met this new function.

For example, in this code, function _construct used to instantiate a new class.

Class Car{

    private $brand;
    private $type;

    public function __construct($merk, $type){
        $this->brand= $brand;
        $this->type = $type;
    }

    public function getCar() {
        return "<strong>Brand:</strong> " . $this->merk . "<br /> <strong>Type:</strong> " . $this->type;
    }


}

But I'm found new example which only contain this code :

Class Game{
    public function __construct()
    {
    echo "Blah";
    }
}

Why on the last example, function __construct just contain the word echo "Blah" not contain this code too --> $this->whatever word ?

Jan B. Kjeldsen
  • 17,817
  • 5
  • 32
  • 50
Kevin
  • 79
  • 2
  • 11
  • Read some OOP book before going on, as __construct is only the first of many differences you'll meet between OOP and procedural paradigms. – xlecoustillier Oct 08 '14 at 07:29
  • http://php.net/manual/en/language.oop5.decon.php#object.construct You can read documentation. – mochalygin Oct 08 '14 at 07:29
  • Take a look at the PHP manual - there's a description of constructors [here](http://php.net/manual/en/language.oop5.decon.php) –  Oct 08 '14 at 07:30
  • possible duplicate of [What is the function \_\_construct used for?](http://stackoverflow.com/questions/455910/what-is-the-function-construct-used-for) – Sal00m Oct 08 '14 at 07:30
  • It's a function that is automatically called whenever you instantiate an object, and there is no corresponding element in procedural PHP, because procedural PHP doesn't use objects – Mark Baker Oct 08 '14 at 07:30
  • I have read some reference include this question before "What is the function __construct used for? ", but i still don't understand. maybe you can give me easy example with explanation in it? – Kevin Oct 08 '14 at 07:40
  • If it's difficult to convert __construct function into procedural php, maybe you can give me an example in vb 6 that have same function with function __construct in php – Kevin Oct 08 '14 at 08:02

2 Answers2

2

The __construct method is usually the method called in a class when an instance of that class is created (called an object). The method is called the constructor because it constructs the object, but that doesn't mean you must use the method to do so.

The point of the method is to set the variables of the object to their inital values, which may be a primitive type such as an integer, or perhaps an instance of another class. If the variables are to be something other than a default value, the __construct method can accept arguments to set the values of the variables.

You don't have to set the variables using the constructor method, nor is it the only thing you can do. You could, for example, print a success message or call another method, either in that class or elsewhere. So your two examples are both valid. The first is how you would expect the constructor to be used, but the second is still vaild. Like I said, __construct is the method that is called on creation of the object, which doesn't mean you have to use it for the intended purpose.

In PHP, it is not required to have a constructor method in a class, but in many other object orientated programming languages, such as Java, if a constructor is not present then it will produce an error at compile time. This is because PHP is what is know as a weakly typed language, which has many pros and cons which you can research further.

Because __construct is one of the magic methods specifically used in a PHP class, it cannot be converted into procedural code. To call the method with arguments, when you assign a class in your code you should give your arguments in the creation statement, like this;

$object = new MyClass($arg1,$arg2);

You cannot call the constructor from outside the class, other than when creating a new object. The only exception to this is a child class (ie, a class that extends another class) can call the constructor of its parent using parent::__construct();.

worldofjr
  • 3,868
  • 8
  • 37
  • 49
1

The constructor is meant for to instantiate a new Class (not neccessarly though) like the example below:

<?php
Class Car{

    private $brand;
    private $type;

    public function __construct($brand, $type){
        $this->brand= $brand;
        $this->type = $type;
    }

    public function getCar() {
        return "<strong>Brand:</strong> " . $this->merk . "<br /> <strong>Type:</strong> " . $this->type;
    }


}


$car= new Car('Audi', 'A3');
echo $car->getCar(); 
/* Will return:
 Brand: Audi
 Type: A3
*/
Daan
  • 12,099
  • 6
  • 34
  • 51