PHP framework class - How to initiate classes via class - php

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/

Related

Creating instances of classes in CodeIgniter

Without using CodeIgniter I would normally just do;
require_once("object");
test = new object();
How would I go about doing this in CodeIgniter?
Edit: for example this class could be a video game object. It might be holding a number of variables, for example title, age,description etc. There would also be variable get/set methods for the above variables.
For example, I might use this class to help contain the information created by a database search.
Codeigniter uses the Singleton design pattern and most of your classes are loaded using the loader class (as needed, in a constructor, or in the autoload config file) and are then available via the Codeigniter Super Object $this->my_model. As Kai Qing noted, using a Model would typically entail:
// In the constructor, controller method, or autoload
$this->load->model('my_model');
// Then to use a method simply
$this->my_model->my_method();
In Codeigniter classes are more like utility classes to group like functionality. However, you can always use native PHP in Codeigniter to require a class and then instantiate your own objects.

What is the difference between Modules in js and Classes in php?

I am learning about modules online and it seems like modules in js and classes in php are very similar. Both group functions together for easier to understand coding. Functions can be declared in both and made public or private. How are they similar in use and how are they different?
Javascript's modules provides some nice features like encapsulation, the private state and even inheritance from other modules. While they provide some of the features of classes, as in PHP, they are not. They try to build on the existing Javascript functonality to emulate classes, hence why the confusion. i.e. they are built to look and feel like classes.
Javascript's modules are instances of an anonymous function assigned to a variable. Therefore they have all the features of a function where their code is executed top to bottom, they have and sometimes use a return statement (in PHP classes no statements can be run directly apart from field definition and assignment) and they even have access to global variables. In PHP, on the other hand a class, or rather it's methods, cannot access a variable that is not in the class itself. In order to access global variables a class method or static function has to explicitly call the variable i.e global $a inorder to import it. In js modules, all global vars are accessible but sometimes one chooses to explicity import them for neater code (function(a){})(imported);
Another important issue is data abstraction. While js modules provide private states for the fields, PHP's classes, just like C++, java, python etc, provide more security to the properties. It allows for base classes using the abstract class and interface keywords whereby class methods and attributes are only defined or structured but not used.
PHP classes also have constructors and destructors, that are called when the class object is initialized and on the last mention used to destroy the object. Granted, you can create functions in modules to run when you want, in PHP on the other hand, functions in the method are only executed when they are called either by the object, the class or other functions.
In classes there are static functions, these can be called without even having an object of the class and run independent of objects, on the other hand in js, everything is an object; which defeats the point of static functions.
They are similar in that: both have inheritance, where you can extend an existing module with a new one, and in PHP you can use extends to inherit from a parent class. They both have private data states preventing external access, they both group and package data and methods, and both are awesome when utilized properly.

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

Where is the $db in CI_Model?

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.

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