Welcome Guest, Not a member yet? Register   Sign In
Constructors???
#1

[eluser]Tom Taila[/eluser]
Can anyone tell me what constructors are actually used for? the descriptions im getting of many different websites arent getting through to me so if anyone could put it in simple terms or give a half decent example i would be very grateful.
i keep seeing:

parent:: Controller();

or

parent::__construct();

everywhere but i dunno what it is or does? and is it absolutely nessesary or can you manage without ever using it?
#2

[eluser]danmontgomery[/eluser]
It's a function that gets called with the object is instantiated. parent::{class name} and parent::__construct() are identical, the latter being php5 only. If you don't define one, the parent's is automatically called, so they are only necessary if you are doing anything other than what's in the parent constructor.
#3

[eluser]Tom Taila[/eluser]
[quote author="noctrum" date="1282081982"]It's a function that gets called with the object is instantiated. parent::{class name} and parent::__construct() are identical, the latter being php5 only. If you don't define one, the parent's is automatically called, so they are only necessary if you are doing anything other than what's in the parent constructor.[/quote]

hmm im still not 100% sure i get it, sorry i know im being a pest, but you said 'its a function that gets called when the object is instantiated.' But where is this function defined? do i define this somewhere manually prior to using the parent::[classname] function or is it a preset function which has a specific use??
you also said 'only necessary if your doin anythin other than whats in the parent constructor.' Stupid question probably but what is the 'parent constructor' , i dunno what a constructor is?? :S sorry again for the bother and thanks a lot Smile
#4

[eluser]danmontgomery[/eluser]
It's defined the same way any other class method is defined. Naming a function identically to the class name is defining a constructor. You can also call that function __construct(), either will work.

Code:
class my_class {

    function my_class() {
        // constructor
    }

    function some_function() {
         // not a constructor
    }

}

The difference being that my_class::my_class() is called when the object is created, and my_class:Confusedome_function() has to be explicitly called. By extending a class (the basis of the CI framework), you are inheriting all members and methods of that class, including the constructor, so:

Code:
class parent_class {
    function __construct() {
        echo 'a';
    }
}

class my_class extends parent_class {

    function my_class() {
        parent::__construct();
        echo 'b';
    }

}

$obj = new parent_class(); // Outputs 'a'
$obj = new my_class(); // Outputs 'ab'

When working with CI, the object is instantiated when you call load().

Code:
$this->load->model('my_model'); // Calls my_model::my_model(), or my_model::__construct(), whichever you have defined

So, to answer your initial question, you only need to worry about constructors if there's work you need to do when the object is instantiated in addition to the work CI is already doing.

http://ellislab.com/codeigniter/user-gui...aries.html
http://www.php.net/manual/en/language.oop5.decon.php
http://www.php.net/manual/en/language.oo...itance.php
#5

[eluser]Tom Taila[/eluser]
alright, thats all been taken in imma try use it myself now thank you so much bro




Theme © iAndrew 2016 - Forum software by © MyBB