1

I can't quite understand the OOP here. What I believe is that $this or this can be used to point to the current instance of the class and has access to all non-static members. But then what is happening here:

public function store(Request $request)
{
    $this->validate()
}

This code is from the controller class of the Laravel framework. I can have access to validate() method but the problem is, its not in the current class and even if its being inherited from the base or parent class, I shouldn't be able to access it through $this variable.

And later in the code I was able to use my Model like this:

$post = new Post;

Why did I call the Model Post class and not the constructor of that class?

Waleed
  • 1,097
  • 18
  • 45
  • 1
    `$this` works through the inheritance tree, with access to public or protected methods in parents, grandparents, great-grandparents, etc; so why do you believe `and even if its being inherited from the base or parent class, I shouldn't be able to access it through $this variable.`? – Mark Baker May 23 '16 at 10:15
  • Check this link, it might be helpful, http://stackoverflow.com/questions/1523479/what-does-the-variable-this-mean-in-php – Anik May 23 '16 at 10:16
  • As for `$post = new Post;`.... the constructor is called automagically when you instantiate a class – Mark Baker May 23 '16 at 10:25

1 Answers1

5

What I believe is that $this or this can be used to point to the current instance of the class and has access to all non-static members.

That is correct.

I can have access to validate() method but the problem is, its not in the current class and even if its being inherited from the base or parent class, I shouldn't be able to access it through $this variable.

This is wrong.

The validate method comes from a trait (found in Laravel\Lumen\Routing\ProvidesConvenienceMethods). If a controller uses the trait, and you extend the controller - you inherited the methods from this "base" class.

Mjh
  • 2,904
  • 1
  • 17
  • 16