Jump to content

Variable Functions, Class Methods, and the $this Object


iwato

Recommended Posts

Question ONE: What is the scope of the $this object in class Foo below? Does it only apply to the variable( ) method?Question TWO: Is the variable $funcname every anything more than a string-type variable? The is_string() function returns 1 when applied both before and after the $foo -> $funcname(); statement.

<?phpclass Foo {	function variable() {		$name = 'bar';		$this->$name();	}	function bar() {		echo "This is Bar";	}}$foo = new Foo();$funcname = "variable";$foo->$funcname();?>

Roddy

Link to comment
Share on other sites

Question ONE: What is the scope of the $this object in class Foo below? Does it only apply to the variable( ) method?
$this is defined for all methods in an instance of a class.
Question TWO: Is the variable $funcname every anything more than a string-type variable?
No. Your aren't modifying it after the initial initialization.
Link to comment
Share on other sites

No. Your aren't modifying it after the initial initialization.
Thank you, Synook, for the quick reply to both of my questions.Just a little follow-up. Can the same be said for the $name variable in the variable( ) method of the previous example?Roddy
Link to comment
Share on other sites

Just a little follow-up. Can the same be said for the $name variable in the variable( ) method of the previous example?
Yes, again you aren't making any changes to it after you initialize it.Edit: You're also declaring it within the scope of the function. So it's a variable that will only be available inside the variable() function. If you were to declare it in the class namespace it would then be a property of the Foo class.
class Foo {	$name = 'bar';	function variable() {		$this->$this->name(); //Not sure if this is how it works or not.  Didn't test it.	}	function bar() {		echo "This is Bar";	}}

Link to comment
Share on other sites

class Foo {	$name = 'bar';	function variable() {		$this->$this->name(); //Not sure if this is how it works or not.  Didn't test it.	}	function bar() {		echo "This is Bar";	}}

$this->$this->name();This appears unusual, but it is a point worth considering.Indeed, why would it not more simply be as follows?name();After all, aren't both variable() and $name within scope of the same class?Roddy

Link to comment
Share on other sites

$this->$this->name();This appears unusual, but it is a point worth considering.Indeed, why would it not more simply be as follows?name();After all, aren't both variable() and $name within scope of the same class?Roddy
Yes both variable() and $name are within the scope of the class. However, when you reference a property or method of a class from within the class you need to include the $this keyword. It's the same as referencing the same property/method from outside of the class.Referencing the property as name() without $this-> will throw an undefined t-constant error. (I think that's the error it throws)
Link to comment
Share on other sites

Did some testing with the code I supplied above. It appears that $this->$this->name; does not work. The code would actually look like this:

class Foo {	private $name;		public function Foo() {		$this->name = 'bar';	}	function variable() {		$funcName = $this->name;		$this->$funcName();	}	function bar() {		echo "This is Bar";	}}

Link to comment
Share on other sites

Just tested that out and it does indeed work. :)
Come on you guys. Stop it! You are confusing the issue. I can achieve all of the same by simply writing the following:
<?php	class Foo {		function variable() {			$funcName = ($this->name = 'bar');			$this->$funcName();		}		function bar() {			echo "This is Bar";		}	}	$foo = new Foo();	$foo->variable(); // Output: This is Bar?>

Now, why does this work? Is it not because ($name = 'bar') and $this are equivalent until $this becomes $funcName()? In short, $this refers to the functional object of the variable() method. Nothing more and nothing less!Roddy

Link to comment
Share on other sites

$this represents the instance of the class, the actual object. The code above works because when you run $this->$funcName(), PHP assumes that the name of the function is in the variable $funcName, so it uses that as the name of the method that you're trying to run in $this. That's the exact same reason this works:$this->{$this->name}()I'm not trying to confuse the issue, I'm trying to explain this to you. The reason that works is because the brackets make it unambiguous to PHP that the name of the method is in $this->name, and you want to run the method of that name in $this. You can assign that to another variable if you want first, but it's not necessary.

Indeed, why would it not more simply be as follows?name();After all, aren't both variable() and $name within scope of the same class?
They are members of the same object, but so what? Assume you create a class and define a method called isset in the class. How are you supposed to tell PHP if you want to run the global predefined isset or the method on your class? You would either need to do $this->isset or something like global::isset, and the designers of PHP thought it was better to qualify the methods on the objects instead of the global functions. As far as I'm aware this is how the majority of object-oriented languages are designed.
Is it not because ($name = 'bar') and $this are equivalent until $this becomes $funcName()?
I'm not sure what you mean by that. $this and ($name = 'bar') are not considered 'equivalent', and $this does not become $funcName. What this line does:$funcName = ($this->name = 'bar');is work from right-to-left, so first it assigns the value 'bar' to the variable $this->name (which hasn't been declared in the class, but it probably just makes either a new public or private property). So "($this->name = 'bar')" is considered an expression, and every expression evaluates to a specific value. For an assignment expression, it evaluates to whatever value was assigned. So that expression evaluates to 'bar', and then the value 'bar' gets assigned to the variable $funcName. So the end result of that line is that $funcName gets assigned the value 'bar', and a side effect is that $this->name also gets assigned the value 'bar'. It doesn't link $funcName and $this->name in any way, it just assigns the same value to both of them. This would also work:$funcName = 'bar';$this->$funcName();
Link to comment
Share on other sites

$this represents the instance of the class, the actual object. The code above works because when you run $this->$funcName(), PHP assumes that the name of the function is in the variable $funcName, so it uses that as the name of the method that you're trying to run in $this. That's the exact same reason this works:$this->{$this->name}()I'm not trying to confuse the issue, . . .
A brief explanation may be required here. When this topic first started I asked the question, "What is the scope of the $this object in class Foo below? Does it only apply to the variable( ) method?" In reply, Synook answered, "$this is defined for all methods in an instance of a class". I interpreted Synook's reply to mean that $this was like a magical constant that could be reapplied in any method, but whose meaning changed within the scope of each method. What Synook apparently meant, however, is that $this always refers to the same entity -- no matter the method in which it is found. My interpretation suggests that an object -- in addition to instances created from a given class -- is created for each function. These are two very different concepts.After this now somewhat lengthy discussion, I have come to realize that $this always refers to an instance of the class that was used to define the object to which $this refers. Under the assumption that my confusion has dissipated I would like to ask another follow-up question.QUESTION: Which of the following statements are true in regard to the arrow -> operator and pseudo $this variable? If a statement is in error, please explain why.1) The arrow operator can be used to access both the properties (variables and constants) and methods of a class.2) The arrow operator can only be applied to an instance of the class from which the instance was created.3) Within a class the pseudo $this variable always refers to an instance of the class that has yet to be created from the class.4) Outside of the class the $this operator has no meaning and is necessarily replaced by the name of the instance created from the class.Roddy
Link to comment
Share on other sites

1) The arrow operator can be used to access both the properties (variables and constants) and methods of a class.2) The arrow operator can only be applied to an instance of the class from which the instance was created.3) Within a class the pseudo $this variable always refers to an instance of the class that has yet to be created from the class.4) Outside of the class the $this operator has no meaning and is necessarily replaced by the name of the instance created from the class.Roddy
1. Yes. In fact, it MUST be used to access methods and properties, except in the case of static members, where the form "classname::member" may be used instead.2. Too much terminology for me here... I'm not sure if you're right... What I like to keep in mind is simply that the arrow operator must be used on variables of type object. Every time I use the term "class" or "instance", I slowly get confused.3. Yes. Terminologically speaking, that's probably not exactly correct, but that's exactly the case in practice.4. I'd say the name of the variable holding the object, but yeah.
Link to comment
Share on other sites

2. Too much terminology for me here... I'm not sure if you're right... What I like to keep in mind is simply that the arrow operator must be used on variables of type object. Every time I use the term "class" or "instance", I slowly get confused.
I am happy to learn that I am not the only one who eventually suffers from the lack of clarity among authors who write about classes and the objects that they generate. Fortunately, I was weaned with Adobe's ActionScript where the problem appears to be much worse -- still another reason for Apple to have forsaken Flash programming.
4. I'd say the name of the variable holding the object, but yeah.
This refinement does help, by the way.Roddy
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...