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

2
  • http://php.net/manual/en/language.oop5.php this should help clear some confusion... Commented Jul 24, 2012 at 8: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" ;) Commented Jul 24, 2012 at 8:47

5 Answers 5

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?

Sign up to request clarification or add additional context in comments.

1 Comment

No, $this is not a keyword... /// Better now ;)
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"

Comments

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!

Comments

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();

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.