Constructors??? |
[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?
[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.
[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 ![]()
[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 { The difference being that my_class::my_class() is called when the object is created, and my_class: ![]() Code: class parent_class { 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
[eluser]Tom Taila[/eluser]
alright, thats all been taken in imma try use it myself now thank you so much bro |
Welcome Guest, Not a member yet? Register Sign In |