Welcome Guest, Not a member yet? Register   Sign In
__construct doesn't work?
#1

[eluser]jorgeguberte[/eluser]
Hello.
Yesterday i tried to declare some variables using the __construct() approach, but as soon as i did it, everything else stopped working.
What can i do to have variables that are accesible through the Class' scope?

--edit--
Poking around i found a solution (yes, i should try more things before posting a topic).

Code:
class Neptune extends Controller{
private $variable = 'value';

function function_here(){
  echo $variable //it works.
}
//everything else
}

I don't know if this is the best approach, but as long as it works, i'm game. Smile
#2

[eluser]designfellow[/eluser]
Hi,

Create a function with the name of the class..

It will act as constructor function

like

function Neptune()

{
parent::Controller();
private $variable="value";
}


Happy Coding,
DesignFellow
#3

[eluser]jorgeguberte[/eluser]
Great!
That's exactly what i wanted. Thank you. Smile
#4

[eluser]GSV Sleeper Service[/eluser]
did you call parent::__construct() in your constructor?
#5

[eluser]jorgeguberte[/eluser]
[quote author="GSV Sleeper Service" date="1250714420"]did you call parent::__construct() in your constructor?[/quote]

Hmmm, no. How would it be, exactly?
It's nice being able to see how people cope around an issue using different solutions. I code for fun only, so my knowledge is a bit stone-aged.
#6

[eluser]LinkFox[/eluser]
[quote author="jorgeguberte" date="1250719645"][quote author="GSV Sleeper Service" date="1250714420"]did you call parent::__construct() in your constructor?[/quote]

Hmmm, no. How would it be, exactly?
It's nice being able to see how people cope around an issue using different solutions. I code for fun only, so my knowledge is a bit stone-aged.[/quote]

Basically you can't call parent::__construct() in CI because it's PHP 4 you have to use parent::Controller() etc.

parent::__construct() is the right way to do it in PHP 5




Theme © iAndrew 2016 - Forum software by © MyBB