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

[eluser]Unknown[/eluser]

Hey everyone,

My first post Smile I have decided to learn CI and web-development - I'm new to this world, but not new to the world of programming.

I am trying to follow the blog tutorial posted on this website and they create a class constructor with how I would expect it to be, e.g. public function Blog(). When I did it - didn't work. So, after some googling, it turns out to have this format - public function _Construct() .

It seems to be that all constructors of all controllers will have exactly the same name - _Construct(). Now, that's kind of strange to me - it appears to be that the constructors in web development and constructors elsewhere (say desktop and mobile apps) are two different things.

So, please help me understand here - seems to me that OOP is not heavily used in web-development? Or even completely replaced by MVC and the "constructors" are not used to create objects, but rather just initialize default variables? But then again, the constructor inherits from the parent - so there are some inheritance patterns used. Also, if I'm correct - e.g. no OOP - wouldn't it be more useful to initialize variables in the index() function and abandon constructors completely?


Thank you,

#2

[eluser]CroNiX[/eluser]
Creating a constructor named the same as the class name was the old php4 way of doing constructors. Until CI2.0, CI had a backwards compatibility layer that allowed you to use that (or php5 style), which has since been removed in favor of using PHP5 constructors (__construct()) and removing the old php4 compatible code.

You can read more about the native PHP5 __construct() on the php site.

Your comment about putting variable initializations in the index() method would only make them available to that method and not any others in the class. Index() only gets triggered if you go to http://yoursite.com/controller with no method specified, or specifying index as the method. __construct() gets triggered when the class is initialized, regardless of the method called.
#3

[eluser]Unknown[/eluser]
aaaaw, I see Smile that makes sense, thanks alot Smile




Theme © iAndrew 2016 - Forum software by © MyBB