| Re: PHP5 oop question... -
06-02-2007, 08:55 PM
When in-scope and using static functions, use self:
<code>
cl*** 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 cl*** 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 cl***, then you need to either p*** 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." $$ |