Welcome Guest, Not a member yet? Register   Sign In
Variables in class problem
#2

[eluser]JasonS[/eluser]
You really need to learn about OOP.

Code:
class site  {
  public function __construct() {
    $this->example = 'bla';
  }

  public function test() {
    echo $this->example;
  }
}

I would also recommend declaring class variables.

Code:
class site  {
  
  // Contains some example text.
  public $example;
  
  public function __construct() {
    $this->example = 'bla';
  }

  public function test() {
    echo $this->example;
  }
}

__construct is run when a class is instantiated.

You could change __construct for index() however would you then need to do this.

Code:
$site = new site;
$site->index();
$site->test();

With __construct you just need to do this..

Code:
$site = new site;
$site->test();


Messages In This Thread
Variables in class problem - by El Forum - 02-17-2011, 04:51 AM
Variables in class problem - by El Forum - 02-17-2011, 05:10 AM
Variables in class problem - by El Forum - 02-17-2011, 05:39 AM
Variables in class problem - by El Forum - 02-17-2011, 05:55 AM



Theme © iAndrew 2016 - Forum software by © MyBB