0

*update example

have read some articles and from php.net . But still having a problem to understand $this on 'this' example. I understand what is the basic used for .

Let's assume , i have a function that at the end is like this return $this; //nothing come after $this

what is the $this (at the end of the method to) refer to ? how could i check/echo-ing what the $this is

public function check($source, $items = array()){
    foreach ($items as $item => $rules) {
        foreach($rules as $rule => $rule_value){

            $value = trim($source[$item]);
            $item = escape($item);

            if($rule === 'required' && empty($value)) {
                $this->addError("{$item} is required");
            } else if(!empty($value)) {
                switch($rule) {
                    case 'min':
                        if(strlen($value) < $rule_value ) {
                            $this->addError("{$item} must be a minimum of {$rule_value} characters");
                        }
                    break;
                    case 'max':
                        if(strlen($value) > $rule_value ) {
                            $this->addError("{$item} must be a maximum of {$rule_value} characters");
                        }

                    break;

                }
            }



        }
    }

    if(empty($this->_errors)) {
        $this->_passed= true;
    }

    return $this;

}   

*this code is part of register and validation class. But the point is still what the $this at the end refering to . Thanks

MisterCat
  • 1,531
  • 3
  • 13
  • 23
  • 5
    $this refers to the object itself. Read here http://stackoverflow.com/questions/1523479/what-does-the-variable-this-mean-in-php – pbaldauf Nov 02 '14 at 08:55
  • You asked (on a now deleted question) about how to build a PHP code pad. [There's one here](https://github.com/Viper-7/Deployable-PHP-Codepad). – halfer Dec 09 '14 at 08:16

1 Answers1

0

In your example, $this refers to the main class and object.

    class MainClass 
    {
      public function doSomething($x, $y)
      {
         $this->content = "Rssult is: " . $x+$y;
         return $this;
      }

   public function printMe()
    {
         echo $this->content;
    }

}

It returns the Object and the class.

Now what is the usage? One of the fundamental usages is for method-chaining. Well, what do I mean?

Have you ever seen this?

$myClass = new MainClass();

$myClass->doSomething()->print();

The above code is implemented with an approach called method-chaining, since there is a chain of methods (functions) called repeatedly without stop.

Basically, without method-chaining, above code would have been:

$myClass = new MainClass();
$s = $myClass->doSomething(5, 8);
$myClass->print();

But with method chaining, since each method returns the whole class as if you have had new MainClass() , it allows immediate call to the method.

Though it is not exactly new MainClass() since "new" operator creates an instance of the class, while $this returns the current instanced.

In a nutshell, $this says the PHP please return the current INSTANCED copy of the class (whose properties might have been changed with the methods called prior to returning $this).

Mostafa Talebi
  • 8,825
  • 16
  • 61
  • 105