Re: PHP5 oop question...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



When in-scope and using static functions, use self:

<code>

class Static_Ex {
   private static $variable = 'This is a static variable';
   public static function tryStatic() {
       return self::$variable;
   }
}
echo Static_Ex::tryStatic();

</code>

If you think about it, there is no $this in a static function. If static
functions do not need an instatiated object to access class code, you don't
refer to them the same, nor do you use $this internally inside the functions
that are static.

Using static functions and variables is quite tricky. Static methods and
variables make tasks like programming a singleton to point to a single
database connection for all database activities on a website simple and
easy. But if it has to interact with the internals of an instantiated object
within it's own class, then you need to either pass in all variables
(Static_Ex::method($this) when in scope to an instantiated object should
work), and/or make it work entirely on it's own without $this.

--
Jared Farrish
Intermediate Web Developer
Denton, Tx

Abraham Maslow: "If the only tool you have is a hammer, you tend to see
every problem as a nail." $$

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux