Re: Class Variable Scope Question

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

 



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@xxxxxxxxx
[ 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@xxxxxxxxx
[ p ] 323.644.7808


On Mar 15, 2006, at 1:17 PM, Jochem Maas wrote:

Mike Dunlop wrote:
Hi 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 {
        var $variable = 1;


// this works in php4.
<?php

class myClass { function showVar($key = null) {
    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() {
    return myClass::showVar();
}
I am assuming this isn't working because by calling myClass::showVar ()

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).

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/php5

Many thanks on this.
Best,
Mike D
...................................................................
Mike Dunlop
Director of Technology Development
[ e ] miked@xxxxxxxxx
[ p ] 323.644.7808

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[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