0

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!

4 Answers 4

4

Overload the core URL helper class. Add your function as a static method.

2

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.

1
<?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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.