create construct in class inherited by model or controller - php

I'm a java programmer and now I want to learn CodeIgniter framework to apply to my php application. I saw many examples on the web and I have a question. When I create a model or a controller, I have to inherit from CI_Model and CI_Controller but my question is: do I have to always create the construct in every model o controller? So I mean I have to put in every class
function __construct()
{
parent::__constuct();
}

If you don't override the __construct, it is not necessary. But if you override it, you need to call parent::__constuct();, php will not call the parent constructor automatically.

The answer is no.
I don't know how is it in java. But in php if no construct method is found in the child class it will call the parent one

Related

PHP, how to effectively use the functions in a class

I am using codeigniter framework. let say i had controller class named myclass, this class have a function that i want to access from another controller class.
I got troubled when access the function from another controller class, i cant create an instant of this myclass class, it say class 'myclass' not found. i dont want to use include 'myclass.php'; because class myclass have __constructor method, i afraid the content inside __constructor method will conflict with another __constructor.
what the best solution for my case?
Thanks
CodeIgniter framework has a way to automatically loading your custom classes. Look here: http://www.codeigniter.com/userguide2/general/autoloader.html
Just make it possible for the framework to auto-load your class(es) and the problem is solved.

Codigniter 2 - call function from extended class

First disclaimer - I have inhered project, and there is a lot of legacy code which I can't delete (I can delete it, but then I will need to spend few months to write everything from scratch). I extended all the models with custom MY_Model (it is extending the core) which have save function. Also most of old models have save function.
My question is this:
How can I call the save function from MY_Model, and not from the class that is extending MY_Model? Is it possible?
I am unsure about the implementation but when you want to call a static function in current class not the class that extends it you can use :-
self::save();
See it elaborated in https://stackoverflow.com/a/1189663/2489860

CodeIgniter Controller Constructor

I'm very new to codeigniter ,
I wanted to know what is the meaning of a constructor in a controller . I saw the following code in a codeigniter tutorial -
class upload extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper(form);
}
// rest of the class...
My question is when is the constructor invoked - is it called every time the controller serves a request (e.g the controller class is instantiated for each request it receives?)
Well, that's a more general PHP question. Anyway, yes, the magic method __construct() is called (automatically) upon each instantiation of the class, as you can see in the manual: http://www.php.net/manual/en/language.oop5.decon.php
Usually, in CI is not necessary to call a constructor, unless you actually want one. In the example you posted, the code loads the helper on every instantiation of the class - which is the same as loading the helper in every method, just saves a lot of typing and ensures it's not forgotten. You can alternatively put the library/helper/model you want to have alywas loaded in the respective autoload array in config/autoload.php (check "autoloading" in CI's manual)
Once you define a constructor in your child Controller you're compelled to call the parent constructor (of the mail CI_Controller class), because there is where the main CI object is created and all the classes are loaded, and you need those in your child controller too; if fail to do so your child class will construct separately and won't inherit.
I hope I made myself clear, english is not my mothertongue :)
the constructor is magic Literally its called a magic method.
what makes the constructor cool is that it will do things for you BEFORE any of the methods. So if you have an admin class, and someone should be logged in in order to access it - you can check for login in the constructor and bounce them out if they are not authorized.
in the constructor you can load the models, libraries, helpers, etc that your class needs, and they will be available for any method in the class.
you can load in variables that are used by methods. this is really useful for models.
Don't use _construct() function in latest apache & codeigniter
Use helperlin in index() function
That's a general question. Constructor is a function that is automatically called when instantiated. this function helps us to intialize the things that we are going to need frequently in our code like when we have to load the models of helpers like form e.t.c.
$this->load->model('Model_name');
now when you write this line in your constructor you don't need to load this model again and again in your methods of that class.

php - Calling a function from a class within a parent class

I have a parent class, that calls a new facebook class within it. However, I then have another class that extends the parent, and tries to read the facebook class within it, but it dies everytime.
Any ideas?
Thanks!
My psychic debugging skills tell me that your subclass is not calling the parent constructor, so the facebook class is not being created.
If you're in PHP5, try adding parent::__construct(); to the subclass's constructor, possibly with some extra parameters. Otherwise, you might need to do: $this->SuperclassName(); with some extra parameters as well.

Kohana constructor purpose

As far as Kohana is concerned, can you give me a short sentence or two as to WHEN and WHY I need to use a constructor within my controller? After a LOT of reading, I can't seem to wrap my tiny little brain around the constructor concept. Looking for "layman's terms" here. =)
Edit: Question pertains to Kohana v2.3.4
From The Documentation:
If you declare a constructor in your controller, for example to load some resources for the entire controller, you have to call the parent constructor.
public function __construct()
{
parent::__construct(); // This must be included
$this->db = Database::instance();
$this->session = Session::instance();
}
You can see in this example, the documentation demonstrates calling the parent constructor, and then setting up some properties for the class itself that will reference the database connection, and the session.
You see each of your controllers extends the parent controller. For the parent controller to run or import it's functionality into your controller, you need to a constructor in your controller. The parent adds/sets set's functionality behavior of your controllers.
Hope that makes sense, thanks :)

Categories