Welcome Guest, Not a member yet? Register   Sign In
Question about OOP, calling a variable from another method inside the same controller
#1

[eluser]Rubiz'[/eluser]
Hi, I'm going ask something from PHP OOP, not about Code Igniter...

I have a method called "session" and if some parameters are set, this method calls "subsession" method.

But now, I got a change from de client, and I need verify a variable inside "session" method, when I'm using "subsession" method... I thought about something like that:

Code:
if ( $this->session->$variable == '1')

Can I do this?
#2

[eluser]Rubiz'[/eluser]
Well, I had a lot of work but changed my functions... its better...
Thanx anyway!!
#3

[eluser]tonanbarbarian[/eluser]
to reference a property (variables in classes are called properties and functions are called methods) you simple access it like this
Code:
$this->session->variable
The end result of this is that you should be careful. Technically I think you can have a property and method of an object with the same name, but it gets confusing so try to name them differently
#4

[eluser]Lone[/eluser]
For future reference to get a variable from an object (that usually has been defined using 'var' in the class file) you do it as below:

Code:
echo $this->session->variable;

Where as the function has the () afterwards

Code:
echo $this->session->function();

EDIT: beaten by the barbarian Tongue
#5

[eluser]Rubiz'[/eluser]
Thanx guys ^^
#6

[eluser]Edemilson Lima[/eluser]
$this->session is not reserved for the session class? Ex:

$this->session->userdata('xxx');




Theme © iAndrew 2016 - Forum software by © MyBB