Take the 2-minute tour ×

From the function abc();
1. How do I call ‘function a within class A’? ($this->a(); returns error)?
2. How do I access to public variable $bbb?

(- I know the structure is bad but ‘require_once’ part is dynamic etc…)

    class AAA extends CI_Controller 
        public $bbb;

    function ccc ()

    function index ()
        require_once '1.php';

// in 1.php

function abc ()
 // how do i call method a of Class A?
 $this->ccc(); // returns error - Using $this when not in object context in ...
$this->bbb; //

abc(); // etc etc
share|improve this question
Why do you declare $a and function a with the same name? It is a bad practice. –  Andrius Naruševičius May 29 '12 at 6:34
this is a simplified example come on dude... ok I will change the names ;;; –  user706087 May 29 '12 at 6:35
are you calling $this->a() outside of class A? –  Uriel_SVK May 29 '12 at 6:36
I changed the names. Uriel... not outside of class AAA. function abc(); is within index(); which is within the class AAA. I just require_once code from external files. Wow it does look uselessly complicated... –  user706087 May 29 '12 at 6:40
Nesting function definitions is not a good idea... it's going to lead into all kinds of scope resolution issues and readability issues, and is never necessary –  Mark Baker May 29 '12 at 6:45

2 Answers 2

You're trying to do something with require() which it is not designed to do, and doesn't work for.


http://us.php.net/manual/en/function.include.php which also also applies to require().

abc() gets defined in the global scope and thus doesn't have the object context necessary to use $this.

share|improve this answer
When I run index() of class AAA I get 'Using $this when not in object context' error now. Without abc(); - (without $this->) I get 'Call to undefined function' error ;; –  user706087 May 29 '12 at 6:45
Because it's trying to call abc(), which is defined in the global scope. –  Amber May 29 '12 at 6:48
with all due respect, how can I call ccc(); from abc();? (the answer is I cant call the function?) I'm a bit confused. –  user706087 May 29 '12 at 6:54
You can't do what you're doing with require(). –  Amber May 29 '12 at 6:56
thanks. guess i'll have to rethink the solution. The problem is that required code is dynamic (over 100 codes and it's growing), and the required code is hardly in a standard format to group it as a class (or it would take ages to achieve it) –  user706087 May 29 '12 at 6:59

As amber mentioned require wont help here. Couldnt you just pass reference to the function?

function abc (&$ref)

require it outside of class normally ant then just call

function index ()

not sure it will work, but worth a try i believe

share|improve this answer
thanks. I will look into pass by reference. (I'm not very familiar with it, and I can't understand your code above yet). –  user706087 May 29 '12 at 7:07

Your Answer


