index vs. __construct in controller? |
[eluser]skaterdav85[/eluser]
I see that index is the default function executed when a controller is called. Does the __construct magic method have the same effect? Could you have both?
[eluser]InsiteFX[/eluser]
Hi, Not sure if you can use both, give it a try. But in PHP 5: Code: <?php InsiteFX
[eluser]skaterdav85[/eluser]
i guess you can't. I just tried using __construct instead of index() and i got an error that says: Code: Fatal error: Call to a member function view() on a non-object in I wonder why CI chose to use index as the default function instead of __construct. Anyone else know if the __construct magic method has any utility in CI?
[eluser]InsiteFX[/eluser]
Hi, Code: <?php So when you call it it would be controller/your_function_name You could setup a routes file to do it this way You do not have to use the index function InsiteFX
[eluser]skaterdav85[/eluser]
oh ya i know i could do this. I'm just curious if the construct method has any other purpose in CI controllers. Or does index basically replace the construct magic method? __construct is a php 5 addition, and I think CI is built on php 4. maybe this is why...
[eluser]InsiteFX[/eluser]
With the constructor you can pass in array's and other parameters, that your controller may need. InsiteFX
[eluser]TheIgniter[/eluser]
Hi skaterdav, - Classes which have a constructor method call this method on each newly created object - The index method is also called when you execute your controller www.mysite.com/mycontroller/ - In the other hand, when you call a method : www.mysite.com/mycontroller/mymethod, the index method is not called, on the contrary the constructor is called in each method (In each load of the class)... i hope it clear, correct me if i'm wrong.
[eluser]skaterdav85[/eluser]
oh ok ... so the constructor (__construct magic method) is called before index or some other method?
[eluser]TheIgniter[/eluser]
read more about php5.. this the basic thing that you need to know! Code: http://php.net/manual/en/language.oop5.php
[eluser]skaterdav85[/eluser]
well i'm familiar with OOP in php. My question was more related to how CI handles requests. I know that when you instantiate a class, the __construct method is called, and if not found, a method with the same name as the class name is called if it exists. The question is, where does index() fit into all this? I'm pretty sure index() is a CI specific feature. I actually tried having a __construct magic method in a controller and it produces a fatal error, so I guess that answers my question partially. The question is, why? |
Welcome Guest, Not a member yet? Register Sign In |