Welcome Guest, Not a member yet? Register   Sign In
Call a variable outide a different class
#1

[eluser]rochellecanale[/eluser]
Hello just want to ask how can I call a variable outside a different class? For example i have this following code:
Code:
<?php

$username = $this->session->userdata('username');

class MyOwnClass extends anotherClass{

     public function method1(){

         public $getUsername = $username;  //error can't access

    }

}


?>

How can i call the variable outside my own class?
#2

[eluser]Harold Villacorte[/eluser]
Declare it as global:
Code:
<?php

$username = $this->session->userdata('username');

class MyOwnClass extends anotherClass{

     public function method1(){
        
         global $username;

         public $getUsername = $username;  //error can't access

    }

}

?>

Or wrap it in a function:
Code:
<?php

function getUsername()
{
    $username = $this->session->userdata('username');
    return $username;
}

class MyOwnClass extends anotherClass{

     public function method1(){

         public $getUsername = getUsername();  //error can't access

    }

}

?>

Or define it as a constant:
Code:
<?php

define(USERNAME, $this->session->userdata('username'));

class MyOwnClass extends anotherClass{

     public function method1(){

         public $getUsername = USERNAME;  //error can't access

    }

}

?>
#3

[eluser]Otemu[/eluser]
Also to add to the methods above just use:

Code:
class MyOwnClass extends anotherClass{

public $getUsername = $this->session->userdata('username');

     public function method1(){

          echo $this->getUsername ;

    }
     public function method2(){

          echo $this->getUsername ;

    }
}

This method would work on a Class per basis, if you want the variable to be available in several classes, then use My_Controller and have your classes extend that.

Another way is to put variables into your config.
#4

[eluser]LuckyFella73[/eluser]
@Otemu

That won't work:
Code:
class MyOwnClass extends anotherClass{

public $getUsername = $this->session->userdata('username'); // you can't call classes/ methods when setting class properties!

I didn't test but I think you could do it this way:

Code:
class MyOwnClass extends anotherClass{

public $username = '';

public function __construct()
{
  $this->CI =& get_instance();
  $this->username = $this->CI->session->userdata('username');
}
     public function method1(){

         // do something with $this->username
    }
}
#5

[eluser]Otemu[/eluser]
Hi,

Quote:@Otemu That won’t work (LuckyFella73)
Yes you are correct thanks for pointing out my error. You can use my updated code below:

Code:
class MyOwnClass extends anotherClass{
  function  __construct() {
  parent::__construct();
  $CI =& get_instance();
  $this->getUsername = $this->session->userdata('username');
}

public function method1()
{
  echo $this->getUsername;
}

public function method2()
{
   echo $this->getUsername;
}
}
#6

[eluser]CroNiX[/eluser]
Except instead of
Code:
$this->session->
You'd use
Code:
$this->CI->session->
#7

[eluser]LuckyFella73[/eluser]
Don't forget to declare the class property btw.

Code:
class MyOwnClass extends anotherClass{

public $username = '';




Theme © iAndrew 2016 - Forum software by © MyBB