constructor - why, where, when?? |
[eluser]pistolPete[/eluser]
[quote author="boony" date="1234675662"]... when a constructor should be created and where it should be placed and what else should be included in the constructor method.[/quote] From http://php.net/manual/en/language.oop.constructor.php Quote:Constructors are functions in a class that are automatically called when you create a new instance of a class with new.You need to create a class specific constructor if you need additional functionality in it which the parent's constructor does not provide. Have a look at a CI example: Let's say you want to extend CI's Controller class which checks if a user is permitted to access a specific page/functionality of your application: Code: class AuthController extends Controller If you need some libraries / models etc. in many / all of your controller's function, load them in the constructor in order to avoid code reduplication. You also see, if you need something (here the access control) to be done before any other class function is getting executed, put it in the constructor. [quote author="boony" date="1234675662"]Finally, do we use the class name or the __constuct call or are they interchangeable????[/quote] This depends on the PHP version you are using. In PHP4 you can only call the class_name() constructor, in PHP5 you can call both, although __construct() is the preferred one. Have a look at: Constructors and Destructors |
Messages In This Thread |
constructor - why, where, when?? - by El Forum - 02-14-2009, 05:27 PM
constructor - why, where, when?? - by El Forum - 02-14-2009, 06:13 PM
constructor - why, where, when?? - by El Forum - 02-14-2009, 06:13 PM
constructor - why, where, when?? - by El Forum - 02-14-2009, 06:14 PM
constructor - why, where, when?? - by El Forum - 02-14-2009, 06:36 PM
constructor - why, where, when?? - by El Forum - 02-14-2009, 06:40 PM
constructor - why, where, when?? - by El Forum - 02-14-2009, 07:25 PM
constructor - why, where, when?? - by El Forum - 03-01-2009, 10:05 AM
constructor - why, where, when?? - by El Forum - 03-01-2009, 10:34 AM
constructor - why, where, when?? - by El Forum - 03-01-2009, 10:56 AM
|