Where is the $db in CI_Model? - php

I come from a Java background and I am now trying to study PHP for a web project I am doing on the side written in CodeIgniter. Right now I am trying to study how to use CI's models. I am naturally very curious about reading framework code to gain a deeper understanding of how the tools I am using work.
I am really confused as to where the the "$db" in "$this->db" comes from in the Model classes that extend CI_Model come from. I checked the CI_Model class itself but it is not there.
Prior to this I have read the CI_Controller's code and somehow reckon that the although the $this->load->model(....) is not an instance variable ($this->load) with a method model(..._), somehow "it is there" because the constructor instantiates a $this->load instance variable in the constructor (the Loader class found in the core/ folder which has a model(...) function.
But for the $CI_Model I don't see the same thing. Where does the "$this->db" being referenced and when is it loaded?

I've not studied the code hard enought, but I've been using codeigniter for a while, I hope not to say something wrong, but things doesn't work as you guessed. The $db you're looking for is not in the CI_Model superclass (nor in the CI_Controller superclass), but it's a variable declared in the Loader class (system/core/loader.php).
A codeigniter application is designed to work as if it were a giant "superclass", in fact all libraries and models and so on are instantiated using $this (which usually is used in the scope of a class), so when you're loading the db library - and you can do it in your models but also in your controllers (and, if you instanciate the main $CI class in a custom library, also there) - you're in fact referring to a variable create in the loader class, precisely in line 229 (more or less) inside the database method of the CI_Loader class.
I'm not so deep in CI knowledge to give you better details, but if you take a look at the codeigniter.php and loader.php you can get enough insights to underdstand how CI internally works.
If you have other question or something's still not clear just ask ;)

$this->db might be loaded localy in the model/controller: $this->load->library('database') but most likely database library is autoloaded - check: application/config/autoload.php. For the code you should check CI_Loader class like Damien says.

Related

Codeigniter bootstrap like Zend

Is there any way to create a function that works for all controllers in Codeigniter at init?
In Zend there is a application/Bootstrap.php, i need some solution like that.
You could extend the native CI_Controller class and create a MY_Controller class that all of your application's controllers would extend. Methods in the MY_Controller class would then be available to every controller that extends it. You could also put code in the MY_Controller constructor that would be executed each time a child controller was constructed.
I don't remember exactly how the Bootstrap file works in Zend, but if this sounds like a viable solution the Creating Core System Classes section of the documentation explains how to extend the native controller.
You can extend your New_controller to CI_Controller. In New_controller you can write common function which you want. For use about new extended controller you can see this link:
The subject of extending core controllers is discussed briefly in a few places in the manual - specifically in the Core Classes and Creating Libraries pages.
The intent of extending the core Controller is to provide methods and attributes to all your normal Controllers. There are other ways of providing this kind of pervasive functionality - including Hooks, Libraries and Helpers. You should familiarise yourself with the methods and benefits of those alternatives before assuming the following is the answer to your question.
Finally, it’s assumed that you have an application that does something - it doesn’t matter what, merely that you have an existing Controller that we can work with here.
-extend_the_CI_Controller

Class extending Zend_Controller_Action, not found by Zend Tool

I created an class extension of Zend_Controller_Action and added some user defined methods, which will be accessed from any controller so forth.
Every thing is working fine, until I use Zend Tool to create a new Action, as this time The Zend tool will not find out my extended class.
Error Message:
Fatal error: Class 'CMS_Zend_Controller_Action' not found in....
That is the class which extends Zend_Controller_Action and the one extended by other controllers like indexController.
How to make the class discoverable. Do I have to include each and every folders, like my classes are? Does zend does that? I dont think so. How does it do it?
Simple. :-p If it can find your core controllers, then you just need to include the path to your extended controllers.
http://php.net/manual/en/function.set-include-path.php
set_include_path(path_to_your_extended_classes) in your index.php, aka routes file.
I think what you are trying here is not what Zend_Tool is about.
As much as I understand your question and setup you have created a class in your library. Of course, you can extend Zend_Controller_Action with lots of your own classes in your own library/libraries (I do that, too). Adding an action to such a class is maybe unusual but a problem for Zend_Tool for one specific reason.
Zend_Tool I believe is only about the well known structures like /application and same for what is inside /modules. If you create a Controller Class Zend_Tool will do some work for you like adding required folder structure to your /application or /modules folder. Same with action method which require view files. Having a Controller Class in your library does not (should not) need all that and hence is not build into Zend_Tool. I think whatever class you create in your library is not supported in Zend_Tool.

PHP framework class - How to initiate classes via class

I usually setup a initial class on a website called something like baseFrame, which holds all the basics functions. (MVC style)
Now, I realized I was having issues calling the baseFrame from classes called throughout the website (the baseFrame has a function called callClass, which I was using to include and running the new class that extends from the base. I realized that you can't call a extended class under a function, because it's unable to read the parent class.
So, I wrote a new script called "callfunction.php" which was not a class base it self, it was a straight function that called the extended class, which calls the baseFrame correctly. Now this method worked, I however am not used to not using classes. Is there something I'm missing? Would I rather call a new class from within the baseFrame class? Is that possible? Again, running a class within the baseFrame class didn't allow me to call the extended at all...So I'm guessing that isn't technically possible?
may be this resources help.
there are many frameworks to build secure and reliable systems.
note: mvc is just one solution.
http://framework.zend.com/
http://wordpress.com (extensible via plugins)
http://www.codeigniter.com/

Confused About Objects and Classes in CodeIgniter?

I’m fairly new to CodeIgniter and have a question. I’m a bit confused about Classes, Libraries and Objects.
Does CodeIgniter replace the normal PHP way of usings objects i.e. $var = new car(); with libraries i.e. $this->load->library('some_library'); $this->some_library->some_function(); ?
If both are valid, is there a difference? If so, what are the differences and when do I use one over the other? Which is more common/proper?
I am asking because I created a class, but I'm not certain what is the correct manner in which to instantiate it.
Thanks in advance
I am not familiar with CodeIgnitier. But familiar with other PHP frameworks. Most of frameworks use this way for performance improvements, registering things, executing certain events, and making things simpler for developer...
For example if you want to create class "car" with is somewhere in library directory you would have to include the file first before you can create object of that class (miltiple lines of code, more room for error). The framework will create the class and includes related files in 1 line of code (easier and safer).
Framework way also works as a factory. Instead of recreating an object, it will create object only once and every time you call the method again it will return the reference to existing object.
More things are happening behind the scenes when you use framework. Things are getting registered, etc...
CI doesn't replace class behavior per se, it simply adds functionality that allows access to custom libraries/models/views as singleton objects via the core object for simplicity.
Nothing is stopping you from creating (as I have in one of my projects) additional files with classes for non-singleton entities and require them in a model for further use. On hindsight, I should probably have used helpers for this.
What the loader ($this->load) class does, among other things, is it creates a single object of the specified class (model, library or view - not helpers though, see below) and attaches it as a property of the core class that is normally accessible via $this.
Helpers are a bit different. They are not attached, but instead simply 'read' into the global namespace from the point where they are loaded.
To answer your question, it would be more proper to use the loader class in instances where you don't need more than one instance of a class created. If you need 'entity' classes, your best CI-compliant bet would be to create them as helpers.
Given only this context, this looks like Inversion of Control (maybe I'm wrong, I haven't looked too closely at CodeIgniter).
You don't want to rely on the type car as in new car(). What if later you want to make $var a racecar? $var can still do the same things, but it is forced to be a car because you constructed it directly. Or what if you are testing this class, but car is some complex object which calls some external service. You want to test your logic, but don't care if the car service isn't working. So you should be able to change $var to actually load a mockcar. You can't do that if you do $var = new car().
What is Inversion of Control?

Having the option of customized classes but a unified class name

Suppose you are building a web application that is going to be a packaged product one day, one that users will want to be able to extend and customize.
It comes with a core library consisting of PHP files containing classes:
/library/
/library/frontend.class.php
/library/filesystem.class.php
/library/backend.class.php
Now, suppose you want to keep a clean core that users can't patch. Still, you want the user to be able to customize every nut and bolt if need be.
My current idea is to create an autoloading mechanism that, when a class is instantiated, first loads the core include:
/library/frontend.class.php
then, it switches to the user directory and looks whether there is an include of the same name:
/user/library/frontend.class.php
if one exists, it includes that as well.
Obviously, the user include must contain a class definition that extends the definition in the core include.
Now my question is, how would I instantiate such a class? After all, I can always be sure there is a definition of:
class frontend_core
but I can not be sure there is a
class frontend_user extends frontend_core
However, I would like to be able to rely on, and instantiate, one class name, regardless of whether there was a custom extension to the class or not.
Is there a clever way, idea, or pattern how to achieve this?
Of course, I could write a simple factory helper function that looks for the user class first and then for the core class and returns an initialized object, but I would really like to keep this as clean and simple as possible, because as I said, it is going to be a packaged product.
I am looking for a smart trick or pattern that uses as little code, and introduces as little new functionality, as possible.
Why don't you follow the approach as used by Propel? You generate your base classes and already provide an empty User class (extending the base class) where your users can put their overrides/specific implementation details, and in your code you always refer to the User classes. So basically you just use the inverse of the logic you described.
If the explanation above isn't clear, check out http://propel.phpdb.org/trac/wiki/Users/Documentation/1.4/QuickStart#a6.UsingtheGeneratedSQLandOMFiles and generate code for a small database. The base classes are in the om folder, the (by default empty) user classes are in the root folder.
I would implement hooks in the core, so users dont have to hack the core, but are still able to extend the core using hooks
I'd go with using the constructor of the core class to determine the user class to load, and then implement a factory method in the core class to generate instances of the user class. By making the constructor of the user class protected, and having the user class extend the core class you can be sure that code elsewhere cannot instantiate the user class.
C.
I think it's more complicated with a single filename when you want to use inheritance as well. Basically class user_frontend extends core_frontend has to know where to find both classes. Both must be included.
If you just want to do new Frontend you could use PHP5.3's class_alias to point Frontend to the main class to use. Below 5.3. you could use a ServiceFinder, that knows how to map Service Names to Classes and then get the Frontend with $service->get('frontend') or use a Dependency Injection framework.
Edit I removed the Loader code given before, because it was suffering from exactly this problem.
You could have a loader class that will decide which class to instance:
Loader::instance()->load('Frontend')

Categories