Welcome Guest, Not a member yet? Register   Sign In
How to use a variable in: $this->index(); ?
#1

[eluser]chefnelone[/eluser]
Hello.

I'm running this line in a controller:

$this->index(); //it works fine

But I need to use it in this way:

$page = 'index';
$this->$page();// didn't work

or

$page = 'index()';
$this->$page; // didn't work


How can I do this?
#2

[eluser]eoinmcg[/eluser]
you should read up on OOP in php.
Code:
$this->page
refers to a property of the object (i.e. variable)
Code:
$this->page
refers to a method (i.e. function)

Code:
<?php

class Name extends Controller
{

    public $page = 'index';

    public function __construct()
    {
        parent::__construct();
    }


    public function index()
    {
    }

    public function test()
    {
        echo $this->page;
        $this->page = 'whatever';
        echo $this->page;

    }

}
#3

[eluser]mddd[/eluser]
The first example should work:
Code:
$page = 'index';
$this->$page();  // this calls $this->index();

Is this literally what you are doing? If you use class properties, it's a bit different:
Code:
class Someclass()
{
   var $page = 'index';

   function index()
   {
      // hello
   }
  
   function callpage()
   {
      $this->{$this->page}();   // this calls $this->index()
   }
}
#4

[eluser]mddd[/eluser]
You could also user call_user_func(). Check the php manual for that.
Example:
Code:
// call index() with argument 'hello'
call_user_func('index', 'hello');

// call $this->index with argument 'hello'
call_user_func(array($this, 'index'), 'hello');

// call mymodel->index with argument 'hello'
call_user_func(array('mymodel','index'), 'hello');
#5

[eluser]chefnelone[/eluser]
Ok, I see.
I'll remake my question:

Let's say I have 3 functions in a controller. When the index function is called it runs one of the other functions based in the value of $page.

Code:
function index(){
     $page = $this->uri->segment(1); //this can take the values: 'gallery', 'contact', anyOther...

     // if value of $page is 'gallery'
     $this->gallery();

     // if value of $page is 'contact'
     $this->contact();


    //I can't use switch() or if()
}
function gallery(){
    //anything
}
function contact(){
    //anything
}
#6

[eluser]mddd[/eluser]
Didn't I just answer that question?
Code:
function index()
{
   $page = $this->uri->segment(1);
   call_user_func(array($this, $page));
}
function gallery() ...
function contact() ...

Also: it looks to me like you are building functionality that CodeIgniter already has.
Why do you always go to this index method? CodeIgniter is smart about routing stuff directly to the correct controller/method.
You don't have to do that yourself.
#7

[eluser]chefnelone[/eluser]
[quote author="mddd" date="1271340200"]Didn't I just answer that question?

Also: it looks to me like you are building functionality that CodeIgniter already has.
Why do you always go to this index method? CodeIgniter is smart about routing stuff directly to the correct controller/method.
You don't have to do that yourself.[/quote]

It works.

I'm sure you are right, but I'm learning CI and now I need this working. But I don't know how to do what I need with routes.




Theme © iAndrew 2016 - Forum software by © MyBB