lucerias Posted November 10, 2006 Share Posted November 10, 2006 Sorry for asking a beginner question, may i know $this can be only used under class, then can you guys explain $this in an easier way to a beginner like me? If possible, you guys may explain based on a simple example. Thank you for your help. Quote Link to comment Share on other sites More sharing options...
Destruction Posted November 10, 2006 Share Posted November 10, 2006 $this refers to the class itself.Example:[code]<?phpclass A{ private $var; public function getVar() { return $this->var; }}?>[/code]It can also be used to call a function in the same class with same syntax. Say you have a function called setVar that needs to be called internally, you could do $this->setVar();This is a very brief rundown but I hope it helps,Dest Quote Link to comment Share on other sites More sharing options...
btherl Posted November 10, 2006 Share Posted November 10, 2006 More specifically it refers to an instance of a class.If you create two class objects, then $this will tell you which instance is being used.[code=php:0]$first = new A;$second = new A;$first->getVar();$second->getVar()[/code]How does the class know which $var to get? When you call $first->getVar(), $this->var is the $var from $first. When you call $second->getVar(), $this->var is the $var from $second. Quote Link to comment Share on other sites More sharing options...
lucerias Posted November 10, 2006 Author Share Posted November 10, 2006 [quote]<?phpclass A{ private $var; public function getVar() { return $this->var; }}?>It can also be used to call a function in the same class with same syntax. Say you have a function called setVar that needs to be called internally, you could do $this->setVar();This is a very brief rundown but I hope it helps,Dest[/quote]Then may i know what is the usage of private under a class and the meaning of public and non public function. Thank you. Quote Link to comment Share on other sites More sharing options...
lucerias Posted November 10, 2006 Author Share Posted November 10, 2006 [code]<?phpclass Foo{ function Variable() { $name = 'Bar'; $this->$name(); // This calls the Bar() method } function Bar() { echo "This is Bar"; }}$foo = new Foo();$funcname = "Variable";$foo->$funcname(); // This calls $foo->Variable()?> [/code]I tried to add this Foo::Variable(); before ?> and it caused an error. May i know why? If i remove $this->$name(); then the error is fixed. If Foo::Variable(); can call the function directly, why we need to code like this: $foo = new Foo();$funcname = "Variable";$foo->$funcname(); // This calls $foo->Variable()What is the difference? Thank you. Quote Link to comment Share on other sites More sharing options...
Monkeymatt Posted November 10, 2006 Share Posted November 10, 2006 You can actually call it like this:[code]$foo=new Foo();$foo->Variable();[/code]As to not using Foo::Variable(), then it does not have a $this var that is passed to the function, so it will yell at you for referencing $this because it is not defined. Foo::Variable() is for calling static functions that do not require an instance of the class, and cannot reference the current class using $this because there is not current class that it is under.Monkeymatt Quote Link to comment Share on other sites More sharing options...
btherl Posted November 10, 2006 Share Posted November 10, 2006 lucerias, try working with an example which creates two instances of a class, AND which stores data within the class. Then it should be clear why Foo::Variable() cannot use $this.While you are working with class methods only, it won't be clear why there is a difference. Quote Link to comment Share on other sites More sharing options...
lucerias Posted November 10, 2006 Author Share Posted November 10, 2006 [code]$foo=new Foo();$foo->Variable();[/code]Another problem of the above code is why we must create $foo and actually it defines as the class? Is there any way i can directly use the existing class and call function from there? Thank you. Quote Link to comment Share on other sites More sharing options...
Monkeymatt Posted November 10, 2006 Share Posted November 10, 2006 What existing class? There is no instance of the class until you create a variable and define it as of that class. There is only a definition of the class. Think of it this way: what we have before we defined a variable to be the class is like blueprints to a house - you know what it should look like and what it will do when it is made, but it is not made yet. After the variable is defined and set to be the class, it is like the house is built and is now there and you can now do things with it.Monkeymatt Quote Link to comment Share on other sites More sharing options...
Zane Posted November 10, 2006 Share Posted November 10, 2006 Another way of thinking of is....like a till for a registerLike at grocery store, everyone has their own till/register drawerand each person's drawer begins with a set amount of bills.You could call that the Till ClassIt has 100 dollars-10 Ones- 10 Fives- 2 Tens- 2 TwentiesWhen the shift starts there are probably 5 employees that get a new tillso$beth = new Till;$john = new Till;$henry = new Till$zane = new Till;$bob = new Till;5 new instances of a Tilland with each Till you can...... takeFiveDollars()or takeTenDollars()$beth->takeTenDollars();$zane->takeTenDollars();$zane->takeOneDollar();$zane->takeTwentyDollars();$john->giveFiveDollars();eventually each person's set amount of bills will changeand that is the theory behind objects and classes.....all the employees are objects of Till Quote Link to comment Share on other sites More sharing options...
lucerias Posted November 10, 2006 Author Share Posted November 10, 2006 Thank you, i am clear with your example. Another problem of class and function is i tried to create a sum relationship to understand more about class and function. The "First" coding shown as below can work properly whereas i have another idea is define the $val1 and $val2 in function variable() and then $val3 in function foo(), so the calculation will be done in function foo() by passing the value of $val1 and $val2 from variable() to foo(). The "Second" coding is what i have tried to do but there is error, please help me to resolve it. Thank you.First[code]<?php$val1=1;$val2=2;$val3=0;class Foo{ function Variable() { global $val1; global $val2; global $val3; $val3=$val1+$val2; $name = 'Bar'; $this->$name(); // This calls the Bar() method } function Bar() { global $val3; echo $val3; echo "This is Bar"; }}$foo = new Foo();$funcname = "Variable";?> [/code]Second[code]<?phpclass Foo{ function Variable() { $val1=1; $val2=2; $name = 'Bar'; $this->$name(); // This calls the Bar() method } function Bar() { global $val1; global $val2; $val3=0; $val3=$val1+$val2; echo $val3; echo "This is Bar"; }}$foo = new Foo();$funcname = "Variable";?> [/code] Quote Link to comment Share on other sites More sharing options...
Zane Posted November 10, 2006 Share Posted November 10, 2006 you can't grab the scope of another function with globalglobal is meant to extend the scope of variables outside all function and classesI don't exactly know what your question is but I can tell you you're not going to be able to grab val1 and val1 from Variable() by asking for globals in Bar()take a gander at this page and research globalshttp://us3.php.net/global Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.