13

I have a controller with a couple of methods and they all have the same variable within them. I am looking for a way to have them share the same variable. What is the best way to do this?

What I have now:

class Example extends CI_Controller{
  public function test{
    $variable = "awesome";
  }

  public function demo{
    $variable = "awesome";
  }

}
ThomasReggi
  • 55,053
  • 85
  • 237
  • 424
  • 1
    possible duplicate of [PHP/CodeIgniter - Setting variables in __construct(), but they're not accessible from other functions](http://stackoverflow.com/questions/7147555/php-codeigniter-setting-variables-in-construct-but-theyre-not-accessible) – jondavidjohn Aug 22 '11 at 18:42
  • Misleading. Global usually means application scope not class/instance scope. – MJVC Jan 28 '17 at 19:46

1 Answers1

34

How about...

class Example extends CI_Controller
{

  public $variable = "awesome";

  public function test()
  {
    echo $this->variable; // awesome
  }

  public function demo()
  {
    echo $this->variable; // awesome
  }

}
65Fbef05
  • 4,492
  • 1
  • 22
  • 24
  • What is the difference between declaring `$variable` `public` or `private`? – ThomasReggi Aug 22 '11 at 18:45
  • 1
    You can access a public property in an object directly via `Object::variable` or `$object->variable` - A private property you cannot and can only access it from withing the class. – 65Fbef05 Aug 22 '11 at 18:46
  • 2
    public/private controls whether or not inheriting classes derived from this one have direct access to this property. – jondavidjohn Aug 22 '11 at 18:47
  • @ThomasReggi usually people learn OOP before using frameworks. – tereško Aug 22 '11 at 18:48
  • Someone else posted an answer reguarding `__construct()`. I am reasonably new to codigniter and have been using for about a week creating controllers without a `__construct()` I can seam to find any documentation on what it actually is used for or does. Can someone point me in the right direction? (for example how would I load a model or library once in a controller? using `__construct();`) – ThomasReggi Aug 22 '11 at 18:49
  • @teresko I think immersion and practical programming is the best way to learn. If you know of any helpful php OOP docs / tutorials, they would be greatly appreciated. – ThomasReggi Aug 22 '11 at 18:51
  • 2
    Here's a good read on OOP as it applies to PHP: [http://php.net/manual/en/language.oop5.php](http://php.net/manual/en/language.oop5.php) – 65Fbef05 Aug 22 '11 at 18:53
  • @ThomasReggi , global variables are part of procedural programing , it is a different methodology. Some books: [PHP in Action](http://www.manning.com/reiersol/), [PHP Object-Oriented Solutions](http://www.apress.com/9781430210115) and "[PHP Objects, Patterns, and Practices](http://www.apress.com/9781590599099) .. and of course stuff on [php.net](http://php.net/manual/en/language.oop5.php). – tereško Aug 22 '11 at 18:58
  • 1
    @ThomasReggi .. and do you really think that masons learn by building skyscrapers ? Leave the frameworks alone till you understand the basics. – tereško Aug 22 '11 at 19:00
  • @ThomasReggi declare your class variables inside the `public function __construct(){ // class variables here $this->classVar = 'example'; }` – b_dubb Jul 07 '16 at 17:14