[eluser]Aea[/eluser]
Okay, so this might be more confusing then you probably want, but it's important, see the code I wrote below to gain a better understanding of class scopes, but basically in this case all you want to do is make $CI accessible everywhere in your class, now the proper method to do this would be...
Code:
class Template
{
protected $CI; // This is optional but recommended
function __construct()
{
$this->CI =& get_instance();
}
function show()
{
$this->CI->something...
}
}
Now let's analyze what we're doing here, we're declaring that there will be a protected (i.e. only accessible intraclass and to children), then we're defining it in __construct. Now because we're in a method, we want to tell the class to retrieve that variable for us (our local scope would be within the method, and CI only exists in the class scope), so we use $this->CI to access it and set it as a reference to the master CI class. Then to use it within any method you need to do the same $this->CI, since CI is a class scope variable. You can skip the declaration if you want, but it is good practice to keep limits on variable scope, but if you haven't begun OO programming that will be entirely confusing.
Here's some quick sample code I hacked up to give you an idea about variable scopes.
Code:
<?php
class Template
{
public $classScopeVariablePublicHeader = 'waffle'; // public keyword is optional
private $classScopeVariablePrivate = 'crepes'; // private keyword is mandatory if you want to make it private
protected $classScopeVariableProtected = 'blinz'; // same deal here
public function __construct()
{
$this->classScopeVariablePublicConstructor = 'pancakes';
}
public function method()
{
echo get_class($this).' method()'.PHP_EOL;
echo "Public:\t\t".$this->classScopeVariablePublicHeader.PHP_EOL.
"Public:\t\t".$this->classScopeVariablePublicConstructor.PHP_EOL.
"Private:\t".$this->classScopeVariablePrivate."\t\t // You can see me only in the original class".PHP_EOL.
"Protected:\t".$this->classScopeVariableProtected."\t\t // You can see me only in the original & child class".PHP_EOL.PHP_EOL;
$methodScopeVariable = 'Something Else';
}
}
class FancyTemplate extends Template
{
public function method()
{
echo get_class($this).' method()'.PHP_EOL;
echo "Public:\t\t".$this->classScopeVariablePublicHeader.PHP_EOL.
"Public:\t\t".$this->classScopeVariablePublicConstructor.PHP_EOL.
"Private:\t".$this->classScopeVariablePrivate."\t\t // You can see me only in the original class".PHP_EOL.
"Protected:\t".$this->classScopeVariableProtected."\t\t // You can see me only in the original & child class".PHP_EOL.PHP_EOL;
}
}
$Template = new Template;
$FancyTemplate = new FancyTemplate;
$Template->method();
$FancyTemplate->method();
echo 'Now For main Access (on Template, but FancyTemplate will be the same)...'.PHP_EOL;
echo "Public:\t\t".$Template->classScopeVariablePublicHeader.PHP_EOL.
"Public:\t\t".$Template->classScopeVariablePublicConstructor.PHP_EOL.
"Private:\t"./*$Template->classScopeVariablePrivate.*/"\t\t // You can see me only in the original class (This will cause a fatal error)".PHP_EOL.
"Protected:\t"./*$Template->classScopeVariableProtected.*/"\t\t // You can see me only in the original & child class (This will cause a fatal error)".PHP_EOL.
"Method Scope:\t".$Template->methodScopeVariable."\t\t // You can't access me outside of my method! (This will cause notices)".PHP_EOL.PHP_EOL
;
echo PHP_EOL;
Code:
Template method()
Public: waffle
Public: pancakes
Private: crepes // You can see me only in the original class
Protected: blinz // You can see me only in the original & child class
FancyTemplate method()
Public: waffle
Public: pancakes
Private: // You can see me only in the original class
Protected: blinz // You can see me only in the original & child class
Now For main Access (on Template, but FancyTemplate will be the same)...
Public: waffle
Public: pancakes
Private: // You can see me only in the original class (This will cause a fatal error)
Protected: // You can see me only in the original & child class (This will cause a fatal error)
Method Scope: // You can't access me outside of my method! (This will cause notices)