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.
Related
There are many questions regarding loading custom helper classes in Laravel. However, none of them focus on the loading them with proper initialization.
As of Laravel version 5.3 we can use psr-4 autoloading which is autoloading the entire app/ directory. However, classes are loading but never initialized.
I have my helper class inside the app/helpers/support.php. This class has a constructor, where I want to load some important configuration in order to make the helper usable.
So how can I load my helper but ALSO initialize it properly in Laravel? Right now I am simply working-around the problem by using new \App\Helper\Support(); inside AppServiceProvider.php.
Edit: I'm using the following approach to maintain my helper class:
Best practices for custom helpers on Laravel 5
It seems like what you have is a service. Rather than creating an instance, you can declare it in your app service provider and inject it as a dependency when you need it.
In your register method:
$this->app->bind(\App\Helper\Support::class);
You can now use dependency injection to get an instance of your class. You can also make an instance like this:
app()->make(\App\Helper\Support::class);
If you only want one instance to exist at any given time, use singleton rather than bind.
I recommend reading the service container documentation:
https://laravel.com/docs/5.5/container
is it possible to only instantiate my class once and have the data that has been passed into my class available globally?
In my case I have a class called Mail and I'm instantiating it once on my main page. How can I get the information gathered by the methods available to other parts of my site without having to make a new Mail object?
On some of my files, i need to re include the Mail.class.php because they are outside the scope of the main include php page. But vital information has already been gathered into the already created object Mail. I need to access that information, and I don't want to dump the information into sessions either.
Consider using a Dependency Injection container. The basic principle is what you are describing: define an instance of a class as a service in your container, and allow it to be used from anywhere in your code that has access to the container.
The Symfony framework's DI component is available as a separate component, or you can check out Pimple as a lightweight example. These are two that I use; I'm sure there are many others.
Two options I can think of...
1) Use globals, do NOT do this.
2) Make your mail class a singleton, so whenever you instantiate it, you have the data you set.
is it possible to only instantiate my class once and have the data that has been passed into my class available globally?
Yes, that's possible. Once you instantiated your class, it's an object assigned to a variable. You can pass that variable along in your program and then you have access to the object.
In my case I have a class called Mail and I'm instantiating it once on my main page. How can I get the information gathered by the methods available to other parts of my site without having to make a new Mail object?
This is called dependency injection, other parts of your site have a dependency on objects of even other parts of your site. So you need to inject that dependency, e.g. as a function parameter for a simple example:
Main page:
$mail = new Mail;
other_part($mail);
Other part:
function other_part(Mail $mail)
{
$mail; # there you have it
}
you can post the object in json format to other pages or you could make a temporary file/dbtable to hold the info. or sessions but that was not an option ;-). don't think its possible to do it in an other way. but hey, I'm not an expert.
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/
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?
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')