blackcell Posted June 2, 2008 Share Posted June 2, 2008 How do I make a variable in one class function noticeable to other class functions? I tried global $var but it doesn't seem to help. I remember seeing it somewhere but I can't find the part in php man that shows it. I am kind of new to classes also. Link to comment https://forums.phpfreaks.com/topic/108372-solved-php-classes-variable-scope/ Share on other sites More sharing options...
BlueSkyIS Posted June 2, 2008 Share Posted June 2, 2008 if it's inside a class, you'll need an instance of the class to use it. and if you're going that far, you might as well make it a class variable, callable from outside, sort of like: $some_object = new someClass(); $some_object->$class_variable = 10; // later $c_var = $some_object->$class_variable; Link to comment https://forums.phpfreaks.com/topic/108372-solved-php-classes-variable-scope/#findComment-555555 Share on other sites More sharing options...
trq Posted June 2, 2008 Share Posted June 2, 2008 How do I make a variable in one class function noticeable to other class functions? Are you talking about within the same class? Your question is pretty unclear, but an example (within the same class) would be.... <?php class foo { private $a; function x() { $this->a = 'hi!'; } function y() { echo $this->a; } } $foo = new foo; $foo->x(); $foo->y(); ?> Link to comment https://forums.phpfreaks.com/topic/108372-solved-php-classes-variable-scope/#findComment-555581 Share on other sites More sharing options...
blackcell Posted June 3, 2008 Author Share Posted June 3, 2008 Only to be used inside the class and has a class scope(can be used by all functions within the class). I am wondering... Is this: <?php $this->a = 'hi!'; ?> How you set variables within a class? Or can you use something like this: <?php private $a = 'hi!'; ?> Link to comment https://forums.phpfreaks.com/topic/108372-solved-php-classes-variable-scope/#findComment-556475 Share on other sites More sharing options...
KevinM1 Posted June 3, 2008 Share Posted June 3, 2008 Only to be used inside the class and has a class scope(can be used by all functions within the class). I am wondering... Is this: <?php $this->a = 'hi!'; ?> How you set variables within a class? Or can you use something like this: <?php private $a = 'hi!'; ?> You can preinitialize private variables, so, yes, the following will work: <?php private $foo = "Hi, I'm a private variable!"; ?> You can access/assign to private variables from within the class by using the 'this' keyword, like normal. So: <?php private $foo; public function setFoo($string) { $this->foo = $string; } public function printFoo() { if($this->foo) { echo $this->foo; } else { echo "Foo not set"; } } ?> Link to comment https://forums.phpfreaks.com/topic/108372-solved-php-classes-variable-scope/#findComment-556560 Share on other sites More sharing options...
blackcell Posted June 3, 2008 Author Share Posted June 3, 2008 So when dealing with variables within classes you have to use $this? Link to comment https://forums.phpfreaks.com/topic/108372-solved-php-classes-variable-scope/#findComment-556574 Share on other sites More sharing options...
KevinM1 Posted June 3, 2008 Share Posted June 3, 2008 So when dealing with variables within classes you have to use $this? If they're part of the class/object itself, yes. The 'this' keyword refers to the current instantiated object. So: <?php class Example { private $myGoodies; public function __construct($stringVal = null) //object constructor. $stringVal has a default value of null. { $this->myGoodies = $stringVal; } public function setMyGoodies($stringVal) //function to explicitly set $myGoodies to $stringVal if not done so with the constructor { $this->myGoodies = $stringVal; } public function getMyGoodies() { return $this->myGoodies; } } $example1 = new Example(); $example1->setMyGoodies("This is example1"); $example2 = newExample("This is example2"); $example1_output = $example1->getMyGoodies(); $example2_output = $example2->getMyGoodies(); echo "Example 1: $example1_output -- Example 2: $example2_output"; /* Should print: Example 1: This is example1 -- Example 2: This is example2 */ ?> Link to comment https://forums.phpfreaks.com/topic/108372-solved-php-classes-variable-scope/#findComment-556614 Share on other sites More sharing options...
blackcell Posted June 3, 2008 Author Share Posted June 3, 2008 Ahhh... Ok thanks a ton guys. Link to comment https://forums.phpfreaks.com/topic/108372-solved-php-classes-variable-scope/#findComment-556713 Share on other sites More sharing options...
Recommended Posts