Reputation: 185
I am debugging a site I am having to work with classes which I am not that used to as of yet.
There is this class in this site that processes a $this
but there does not seem to be any variable passed to the class.
the class is like this
class myclass extends otherclass{
function dosmthtomyclass{
print_r($this);
}
}
function dosmttomyclass
prints an array.
there are bunch of variables defined protected in the class but there does not seem to be any specific value specified for any of those variables and there is no constructor in the class to the pass the value to.
I am seriously confused as to where the variable must have been passed from. This may be something really basic but any help would be appreciated. what are the possible ways of passing variables to the class
Upvotes: 0
Views: 112
Reputation: 12831
this is because myclass is getting the data from otherClass... like this:
<?php
class otherclass{
public $name="Mike";
}
class myclass extends otherclass {
function dosmthtomyclass() {
print_r($this);
}
}
$test=new myclass();
$test->dosmthtomyclass(); //prints "[name] => Mike"
Upvotes: 0
Reputation: 25735
$this
refers to the current object. according to PHP documentation
The pseudo-variable $this is available when a method is called from within an object context. $this is a reference to the calling object (usually the object to which the method belongs, but possibly another object, if the method is called statically from the context of a secondary object).
here is some detailed explanation about it. which may help you to understand
What does the variable $this mean in PHP?
Upvotes: 1
Reputation: 14275
Maybe some background on using classes in php can help you:
$this is used to refer to the hierarchy within the class. E.g., you could have a class like this:
class Phpclass{
public function main(){
echo "public function called<br/>";
$this->helloworld();
}
private function helloworld(){
echo "hello world";
}
}
$phpclass=new Phpclass();
$phpclass->main();
This class is a blueprint of an object that will be instantiated with the variables $phpclass. As main() is a plublic function in the class, it can be called from outside the class. The private function can only be called from inside the class, so the main() function uses $this as identifier for the class itself to call the private function helloworld() inside itself. Without $this, the object wouldn't know that you are referring to a function inside itself.
First, the above will echo out "public function called", then "hello world".
Upvotes: 0
Reputation: 706
$this
refers to the current object of the class. Execute the following code for more clarity:
<?php
class abc {
var $val = 3;
function show()
{
print_r($this);
}
}
$ob = new abc();
$ob->show();
Upvotes: 0
Reputation: 1965
You need to go through the manual and/or tutorials on OOP. Because this is the only way you'll understand OOP. Start with this: http://www.php.net/manual/en/language.oop5.basic.php
$this refers to the current instance of the object. Read about PHP+visibility to understand why private varianbles/methods aren't visisble to child classes (extended classes).
Good luck!
Upvotes: 0