Welcome Guest, Not a member yet? Register   Sign In
Call function of a class within a function in the function within the same class - returns 'Using $this when not in obje
#1

[eluser]faluci[/eluser]
(sorry about posting too many questions today... I googled hours for this one but couldn't find the answer Smile

How do I call 'function a within class A' in 'function ab within function index of class A'? ($this->a(); returns error)?
(- I know the structure looks weired but 'require_once' part is dynamic etc...)

Code:
class A extends CI_Controller
{
function a ()
{
}

function index ()
{
  require_once '1.php';
}
}

Code:
// in 1.php
$this->a(); // fine

function ab ()
{
// how do i call method a of Class A?
$this->a(); // returns error - Using $this when not in object context in ...
}
#2

[eluser]Aken[/eluser]
That's a very poor coding structure. You should let us know what you're trying to achieve and we'll recommend something better.
#3

[eluser]faluci[/eluser]
I know it looks terrible, but that wasn't my question btw. Big Grin

I thought of creating custom driver or library but I've come up with the solution as the above so far. I have the situation as the following:
1. there are over 100 dynamic require_onces. i.e. 1~100 (still growing)
2. these dynamic require_onces are used for only one controller (at the moment)
3. $this->a(); is used for all the dynamic require_onces
4. the content of function ab() varies for every dynamic require_onces, but the function name stays the same - as function ab().
5. I simplified the example but there are multiple functions in class A, and in function index()

If I make function ab(); as parallel to function a(), then I have to create call 2 require_onces (and manage 2 files for every dynamic) So I want to manage the dynamic code part in 1 file.

Any suggestion? or plz could anyone answer my original question?
#4

[eluser]Aken[/eluser]
You can't do procedural functions inside a class method, plain and simple.

And if you don't care that your code is awful, then that's all the assistance I'm going to give. Good luck!




Theme © iAndrew 2016 - Forum software by © MyBB