7

There is a method in code igniter under system/core/Common.php called load_class().

I would like to overwrite this method. Usually to overwrite a code igniter class I create a file such as MY_Common.php however in this case Common.php is a collection of methods and there are no classes that encapsulates them.

So how exactly do I do this?

Paramone
  • 2,634
  • 4
  • 31
  • 58
user391986
  • 29,536
  • 39
  • 126
  • 205
  • Why do you want to overwrite that method? – complex857 Apr 05 '13 at 12:26
  • because of http://stackoverflow.com/questions/15833762/running-load-class-on-mthaml – user391986 Apr 05 '13 at 12:28
  • 1
    well, you could just have that two lines of code for your haml include + autoloader registration inside a `MY_Controller::__constructor()` or in a hook and you won't have to fiddle with CI internals. – complex857 Apr 05 '13 at 12:32
  • btw you see in the example the file starts with use do I need to have these? https://github.com/arnaud-lb/MtHaml/blob/master/examples/example.php – user391986 Apr 05 '13 at 12:36
  • We are getting seriously offtopic here, but i would move the example code to `MY_Controller::__constructor` (without the `use`-es) up until line 10 making it like `$this->haml = new Environment('twig');` so in your controllers you can simply do `$this->haml->compileString(...)`; somethinh along the lines of [this](https://gist.github.com/complex857/5319040) – complex857 Apr 05 '13 at 12:44
  • got it :) thanks alot complex857! – user391986 Apr 05 '13 at 12:50

2 Answers2

12

There's no officially supported way to do this by the built in extending mechanisms. Consider some other way to achieve your goal.

However the functions inside Common.php are all wrapped inside an if checking if the function is already exists or not so you can do the following:

  1. Create your MY_Common.php put somewhere in your project (maybe application/core/ to mirror other similar extends)
  2. Open your index.php file in the root of the project
  3. insert include APPPATH.'core/MY_Common.php'; before the closing
    require_once BASEPATH.'core/CodeIgniter.php'; line

Now if you have you have a load_class function in your MY_Common.php it will shadow the original version.

complex857
  • 20,425
  • 6
  • 51
  • 54
  • I see, kind of hackish naturally since it's not supported but could work, thank you. – user391986 Apr 05 '13 at 12:28
  • Ok,nice way, but how can a call the functions when i need to use both of them simultaneously ? In your solution we are losing the original function. – ganji Dec 02 '14 at 15:22
  • @kasra, The short answer is that you can't. PHP won't allow you to have both of them with the same name, and you want your version with the original name (this is the whole point). The easiest way seems to me that you just edit the original file and rename things, or maybe adding a namespace to the file. I'm afraid you are out of luck here with php, maybe if you have [runkit](http://php.net/manual/en/book.runkit.php) you can make it work with [runkit_function_rename](http://php.net/manual/en/book.runkit.php) – complex857 Dec 02 '14 at 17:09
  • @complex857, this hackish solution would work as codeigniter already uses a `if ( ! function_exists('***'))` wrapper for all defined functions in `Common.php` – Ema4rl Jan 04 '17 at 20:16
0

The correct/official way to do that is overwriting the core common function into ie. common_helper.php application/helpers and setting up in config/autoload.php

uruapanmexicansong
  • 3,068
  • 1
  • 18
  • 22
  • 1
    That doesn't work as Common.php is loaded before the helpers, resulting in `Fatal error: Cannot redeclare log_message()...` – Richard Mar 14 '18 at 13:58