Re: Class Variable Scope Question
| From: | Mike Dunlop | Date: | Wed, 15 Mar 2006 22:24:31 +0000 |
| Subject: | Re: Class Variable Scope Question | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-231978@lists.php.net to get a copy of this message | ||
Nevermind - i found the documentation that answers my question - thanks for the help Jeremy and Jochas.
- MD
...................................................................
Mike Dunlop
Director of Technology Development
[ e ] miked@m80im.com
[ p ] 323.644.7808
On Mar 15, 2006, at 1:54 PM, Mike Dunlop wrote:
the fact that you think that it might even be possible that doing myClass::showVar() would cause $this in showVar() to refer to $obj suggests that you don't understand [to some degree] either classes, objects and/or scope (or at least how its implemented/applied in php).No Jochem, I may have not been clear with the phrasing of my question. To simplify, my question was focusing on the scope of "$this". When calling a method via "::" syntax within an outside function, how does that affect the scope of $this (within the class). I appreciate you taking the time to respond to my question. - MD ................................................................... Mike Dunlop Director of Technology Development [ e ] miked@m80im.com [ p ] 323.644.7808 On Mar 15, 2006, at 1:17 PM, Jochem Maas wrote:Mike Dunlop wrote:--PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.phpHi Guys, I am wondering how to be able to call a class variable from within an outside function using the "::" syntax: See an example of the logic I am using below: class myClass {// this works in php4. <?php class myClass { function showVar($key = null) {var $variable = 1;static $defvar = "default", $vars = array("a"=>1,"b"=>2,"c"=>3);if (empty($key)) { return $defvar; } else if (isset($vars[$key])) { return $vars[$key]; }}} function extFunction() {return myClass::showVar();} echo extFunction(),"\n",myClass::showVar("a"),"\n", myClass::showVar("b"),"\n", myClass::showVar("c"),"\n";?>} $obj = new myClass; function extFunction() {don't assume - run the code and find out :-) the fact that you think that it might even be possible that doing myClass::showVar() would cause $this in showVar() to refer to $obj suggests that you don't understand [to some degree] either classes, objects and/or scope (or at least how its implemented/applied in php).return myClass::showVar();} I am assuming this isn't working because by calling myClass::showVar ()instead of $obj->showVar() "$this" would be undefined...does anyone know how to workaround this (e.g. direct access to a variable via "::"it's hard to define a workaround if we don't know what your trying to achieve and why('why' often speaks volumes :-).syntax like myClass::variable). Does PHP5 address this at all?yup. :-) please install php5 straight away :-P php -r ' class T { private static $U = "V"; static function W() { return self::$U; } } echo T::W(),"\n"; ' now start reading here, plenty of nice OO features that blow php4 out of the water in that context: http://php.net/php5Many thanks on this. Best, Mike D ................................................................... Mike Dunlop Director of Technology Development [ e ] miked@m80im.com [ p ] 323.644.7808--PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php