Go Back   Forum Care Forums > Development Reference Area > Php Development

Reply
 
LinkBack Thread Tools Display Modes
Re: PHP5 oop question...
Old
  (#1)
Jared Farrish
Guest
 
Posts: n/a
Default 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." $$

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Contact Us - Forum Care Forums - Archive - Top