a try:
the constructor is the first executed function of a class.
making a new object...
$view = new MyView();
...you call the constructor MyView() of the class MyView indirectly.
the constructor can be used to set certain variables to certain values, or even call another object or another constructor.
class MyView{
var $obj_view;
/* this constructor sets the class variable $this->obj_view
* to $value, which is defined when calling the object by:
* $anyView = new MyView($value); */
function MyView($view){
$this->obj_view = $view;
}
/* if the constructor would not 'predefine' $obj_view,
* this function would print out nothing */
function printView(){
print($this->obj_view);
}
}
to make an object of this class you can go:
$anyView = new MyView("Landscape");
$anyView->PrintView();
instead of using the constructor you could also write a function for that on your own, here: function initVariable()
class My View{
var $obj_view;
function MyView(){
// does nothing
}
function initVariable($view){
$this->obj_view = $view;
}
function printView(){
print($this->obj_view);
}
}
calling the object the difference is, you do 1 call more.
$anyView = new MyView();
$anyView->initVariable("Landscape");
$anyView->printView();
both, the constructor in example 1 and the function initVariable() do nothing but set var $obj_view (or any other class variable) to a certain value in order to be able to work with it in a later function [here: printView()]. without them the function would print out nothing as there is no value assigned to $obj_view.
i would suggest to use the constructor-way, as its shorter and probably easier to handle when inheriting from classes...
please anyone, correct me if i am wrong.