Welcome Guest, Not a member yet? Register   Sign In
Constructors???
#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


Messages In This Thread
Constructors??? - by El Forum - 08-17-2010, 09:56 AM
Constructors??? - by El Forum - 08-17-2010, 10:53 AM
Constructors??? - by El Forum - 08-17-2010, 01:38 PM
Constructors??? - by El Forum - 08-17-2010, 02:29 PM
Constructors??? - by El Forum - 08-18-2010, 08:35 PM



Theme © iAndrew 2016 - Forum software by © MyBB