0

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

Rob
  • 4,927
  • 12
  • 49
  • 54
Narendra Chitrakar
  • 185
  • 1
  • 2
  • 13
  • [http://php.net/manual/en/language.oop5.php](http://php.net/manual/en/language.oop5.php) this should help clear some confusion... – faino Jul 24 '12 at 08:43
  • @diEcho Thats not true: `$this` always refers to the object on which the method is called on. A cannot even imagine any structure, that "refers to all methods and variables" ;) – KingCrunch Jul 24 '12 at 08:47

5 Answers5

1

$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?

Community
  • 1
  • 1
Ibrahim Azhar Armar
  • 25,288
  • 35
  • 131
  • 207
0

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!

Ignas
  • 1,965
  • 2
  • 17
  • 44
0

$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();
KingCrunch
  • 128,817
  • 21
  • 151
  • 173
Kode Plus
  • 696
  • 1
  • 7
  • 10
0

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"
KingCrunch
  • 128,817
  • 21
  • 151
  • 173
MilMike
  • 12,571
  • 15
  • 65
  • 82
0

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

Dennis Hackethal
  • 13,662
  • 12
  • 66
  • 115