Extending codeigniter helper methods - php

I have been developing a project in codeigniter, and I need to change the functionality of a function called site_url() of url_helper. I have already extended the url_helper in my application/helper folder by naming it MY_url_helper, and I can load/access it. What I want to know is, How can I extend the functionality of site_url() function. Do I need to create another method with different name called site_url_ext() in my extended helper, or is there a way to extend the current function.
Thanks.

I think first you need to understand what "Extending" basically means. By extending a helper class, you get exposed to the functionality the helper class is offering, which means that you don't have to re-write it. You cannot extend (as in inherit) a function / method.
If you would like to add more functionality to the already existing site_url() function, you can create your own separate function and name it as you wish e.g. site_url_ext(), copy the code in the original site_url() if need be and start modifying from there on-wards. Alternatively code your own site_url_ext() altogether.
If it's just the base_url you would like to modify then do only that in the constructor of the url_helper class.

Related

Need advice with design - changing static into normal methods

My whole project is basically divided into two parts:
core
helper classes
User creates his custom classes and uses methods from helper classes in there like:
\Project\System\Helpers\Class::foo();
So every public method in each helper class is declared as static. I've came up with an idea to change this, make all user custom classes inherit one special class:
class SingleBeingInheritedClass {
public function helper($class)
{
return new \Project\System\Helpers\$class; // it's just to show the idea
}
}
so that instead of calling static function, user could write:
$this->helper('class')->foo();
The problem is I use some of these helper classes inside a couple of core classes. And I don't want core classes to inherit anything related to helpers.
In these core classes I also don't want to make the code longer and initialize objects in every method using these helpers.
How should I handle this? Or maybe static methods aren't that bad here?
You wrote:
I also don't want to make the code longer and initialize objects in
every method using these helpers.
I you would like to avoid instantiating objects, then you shall stick to static methods. In my projects I use static methods for helpers, for the exact same reason.
These helper classes are then used as 'function libraries'. In this case, class is more like a namespace for the helper functions, not something which gets instantiated.

The management of files and folders in codeIgniter can be made from controller or model?

I want to verify if file/folder exists on my CI system (not system folder), can i do it from controller or I need create a method on my model to do it?
It's basically a matter of choice. You can define your function in both your model and controller. But if you want to use it over and over from different controllers, then better define a new model and put that function in it. I'd define model which is to be used by many controllers that is having some common-general methods. Just to keep things apart and maintaining them later. Helpers are also a way to keep things separate.
You have got many way for this :
You can create an helper and put your function like verify()
You can create a private function into your controller like
private function _verify() { }
enjoy !
Best to add this function in a codeigniter helper or in a library class(if you are fan of OOP). Load this automatically with autoload.php and call from wherever you need it.

cakephp components and helpers breaks oop logic! How should I proceed?

I have a string manipulation class that I need in views and in controllers also!
I saw that cake reuses code in Components and in Helpers for this type of situations which on my opinion breaks the OOP logic (eg. Session->read)!
Instead of doing this I created a vendor class which I imported in a StringsHelper and in a StringsComponent. I then created an identical function which instanciates the Vendor/String class and returns the results from the corresponding function. This is not quite inheritance and still redundant, but if I change code in my class it changes everywhere.
Is there a better way to do this?
You do not need to wrap this kind of class in a Helper or a Component.
You could simply create a class with static methods and put it in APP/Lib like mentioned by Mark.
<?php
class StringTool{
public static function manipulate($string){
...
}
}
and then use it in whatever class you need, wether in a Component, a Helper, a Model, etc.
<?php
$s2 = StringTool::manipulate($s1);
I asked this same question before. Best place is in app/Libs, where you can put a class with static helper functions that can be used anywhere in your application, including controllers and views.
Import the class using App::import('Lib', 'YourClass')
CakePHP - Where is the best place to put custom utility classes in my app structure?

CakePHP Manipulate more than one models in the same controller

I created two plugins in the CakePHP, and both of them have the same named model, e.g, plugin1.mod and plugin2.mod.
In the two models, defined the same named method, e.g, mymethod.
Now, I have a controller in my main program. Then, use ClassRegistry::init to initialize plugin1 and can call plugin1.mod without problem.
Problem:
When I use ClassRegistry::init to initialize plugin2 and call the plugin2.mod, it is calling plugin1.mod! Can somebody tell me what wrong here is?
Thank you.
in 2.0 this is not possible (anymore).
class paths are cached (inside App class) and therefore can use a class name only once.
You need to use different class names.

PHP workaround to extend classes of the same name?

I know extending a class with the same name is not possible, but I was curious if anyone knew of a way to load a class then rename it, so i can later extend it with the original name. Hopefully like something below:
<?php
//function to load and rename Class1 to Class2: does something like this exist?
load_and_rename_class('Class1', 'Class2');
//now i can extend the renamed class and use the original name:
class Class1 extends Class2{
}
?>
EDIT:
Well, I understand that this would be terrible practice in a basic OOP environment where there are large libraries of class files. But i'm using the CakePHP MVC framework and it would make great sense to be able to extend plugin classes in this way since the framework follows a well established naming convention (Model names, view names, controller names, url routes (http://site.com/users), etc).
As of now, to extend a CakePHP plugin (eg: Users plugin) you have to extend all the model, view, and controller classes each with different names by adding a prefix (like AppUsers) then do some more coding to rename the variable names, then you have to code the renamed url routes, etc. etc. to ultimately get back to a 'Users' name convention.
Since the MVC framework code is well organized it would easily make sense in the code if something like the above is able to be implemented.
I'm trying to work out why this would be necessary. I can only think of the following example:
In a context that you have no control over, an object is initialised:
// A class you can't change
class ImmutableClass {
private function __construct() {
$this->myObject = new AnotherImmutableClass();
}
}
$immutable = new ImmutableClass();
// And now you want to call a custom, currently non existing method on myObject
// Because for some reason you need the context that this instance provides
$immutable->myObject->yourCustomMethod();
And so now you want to add methods to AnotherImmutableClass without editing either Immutable class.
This is absolutely impossible.
All you can do from that context is to wrap that object in a decorator, or run a helper function, passing the object.
// Helper function
doSomethingToMyObject($immutable->myObject);
// Or decorator method
$myDecoratedObject = new objectDecorator($immutable->myObject);
$myDecoratedObject->doSomethingToMyObject();
Sorry if I got the wrong end of the stick.
For more information on decorators see this question:
how to implement a decorator in PHP?.
I happen to understand why you would want to do this, and have come up with a way to accomplish what the end goal is. For everyone else, this is an example of what the author may be dealing with...
Through out a CakePHP application you may have references to helper classes (as an example > $this->Form->input();)
Then at some point you may want to add something to that input() function, but still use the Form class name, because it is through out your application. At the same time though you don't want to rewrite the entire Form class, and instead just update small pieces of it. So given that requirement, the way to accomplish it is this...
You do have to copy the existing class out of the Cake core, but you do NOT make any changes to it, and then when ever you upgrade cake you simply make an exact copy to this new directory. (For example copy lib/Cake/View/Helper/FormHelper.php to app/View/Helper/CakeFormHelper.php)
You can then add a new file called app/View/Helper/FormHelper.php and have that FormHelper extend CakeFormHelper, ie.
App::uses('CakeFormHelper', 'View/Helper');
FormHelper extends CakeFormHelper {
// over write the individual pieces of the class here
}

Categories