I am new to kohana and also relatively to php.
I have a simple URI encoding function, which mimics that of JS %100, unlike that of PHP's.
I want to use this function in certain classes but I don't want to define it in each and every one of them.
So, how can I define a global function to be used in all places?
(I don't want to create a class for a 3 line code function)
Thank you!
Overload the core URL helper class. Add your function as a static method.
Override your desire to avoid good OO principles. Create a new class and make a static public function. Don't create bad-old global functions.
<?php
function yourFunction( ) {
return 'your result';
}
How about that? Just write that in a file that always gets included and you're good to go. Don't go making a static class just for the sake of thinking you're doing OO, there's no point.
You can put your global function in you bootstrap (APPPATH/bootstrap.php) though I recommend extending the core URL helper class (assuming you're on Kohana 3.x) by creating a new file at APPPATH/classes/url.php:
class URL extends Kohana_URL {
public static function encode($uri)
{
// ...
}
}
It's almost always worth the 3 lines of code. Kohana was designed for you to override its core classes. Your code will be more maintainable.
Related
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.
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?
I am new to PHP and I have a few questions that follows:
Do I need the init function or can I do the job (whatever I need to do in my code) without the init function?
I am saying this because the NetBeans "kinda" created/added automatically the init() function in my project.
In my code I am suppose to create the CRUD functionality in it.
If I don't use it what's the problems I might have and the downsides?
As the official docs would say:
The init() method is primarily intended for extending the constructor. Typically, your constructor should simply set object state, and not perform much logic. This might include initializing resources used in the controller (such as models, configuration objects, etc.), or assigning values retrieved from the front controller, bootstrap, or a registry.
You can have controllers that don't override the init() method, but it will be called under the sheets anyways.
If you are new to PHP, do not start by using a framework. Instead you should learn the language itself.
There is nothing significant about init() function. It is not a requirement for classes in PHP. Hell .. even __construct() is not mandatory in PHP.
That said, Zend Framework executes it right after the controller is created. It is required if you are using ZF.
You can read more about it here.
init() in Zend_Framework for most practical purposes is where you would put code that you need to affect all of the actions in that controller.(at least to test against all of the actions).
For example I often use the init() method to set up the the flashmessenger helper and to set the session namespace I want to be used.:
public function init() {
if ($this->_helper->FlashMessenger->hasMessages()) {
$this->view->messages = $this->_helper->FlashMessenger->getMessages();
}
//set the session namespace to property for easier access
$this->_session = new Zend_Session_Namespace('location');
}
Also Netbeans did not make this method or the controller, Zend_Tool made the controller and the methods utilizing the interface that Netbeans provided. That's why in your PHP settings for Netbeans you have to provide the path to the ZF.bat file and click the register provider button if you change your ZF install.
One more thing...Be aware that there more methods available to the controller that provide hooks into different parts of the dispatch cycle. You may not need them very often but you need to know they are there.
Simply its a constructor for that class(controller)...
init(){
$this->a = 1; //If we set something like this in the init
}
public function fooAction(){
echo $this->a; //1
}
public function barAction(){
echo $this->a; //1
}
ie the variables,objects..that is initialised in init will be available to all the actions in that controller
This is for fun only, don't scream please.
I would like to rewrite the content of a class's methods at runtime (I mean, without modifying the file, only replacing/editing the code in memory), is that possible?
Using reflection, or anything else?
Don't close this question please, I'm looking for another answer than runkit.
Why not simply create a new class that inherits from the one you want to modify and overwrite it's methods?
<?php
class MySimpleXML extends SimpleXMLElement {
public function themethodiwanttooverwrite() {
//...
}
}
?>
As long as the method isn't marked as final...
I want to like something :
if(true)
define("M_STATIC", "static");
else
define("M_STATIC", "");
class A
{
M_STATIC function() // this is not allowed.
{
//do something my task
}
}
I think, you know.... what i want. :)
I'm using a CMS. and new version of
CMS is changed with some function
declaration.(like old cms not have
static but new version have) so i
think, my page should be compatible
both version (this class extends by CMS class and this function is override to parent function )
This is just not possible, actually : a method is static, or is not ; but it's something thats defined at compile-time, and not at execution-time.
And, in PHP, there is no pre-processor (like you'd have in C, for example), to do the kind of replacements you are asking.
Since the new version is static but the old version wants non-static, I'd define it static and then create an instance method that simply calls the static one for backward compatibility.
I am not sure doing something like this is possible(Which I do not think so) , even if it is possible ,It is not a good practice in OOP, it will make your code very difficult to understand and adds complexity to it.
I am not sure what you want out of this bit if you want need a static function create a class to hold a static one and another class which holds non static functions.