-4

$Clint_ip=$this->request->clintIp();

May I get a clear concept about this line?In here I know $clint_ip is a variable,but what is the next three?which one is an object? which one is a method? which one is a class?

I just need to understand this line.In several project I have seen this types of line.In this line which one called object?If you want You can give another example.In here $this is an object?or class?or method?

Mutawe
  • 6,464
  • 3
  • 47
  • 90
Alimon Karim
  • 4,354
  • 10
  • 43
  • 68

3 Answers3

2

Yes $Clint_ip is an variable,

  1. Like other object oriented based programming languages $this is the this of a class consisting it. (For more about this When to use self over $this?)
  2. request looks like an object of another class
  3. and clintIp() is the public method of the class of the request object
Community
  • 1
  • 1
zzlalani
  • 22,960
  • 16
  • 44
  • 73
1

The code you provided appears to be from inside of a class.


A class is denoted like this:

class Example {
    private $foo;
    public $bar;

    public function __construct() {

    }

    public function method() {

    }

    private function other() {

    }
}

When you create an object of this class, you can use the format:

$example = new Example();

This calls the constructor __construct().

Once you have created ("instantiated") this object, you can use the -> to call the properties of the object.

So, I can say

$example->bar = "Foo"; 

which sets this property to a string.


Your Code

In your code, the property "request" is itself an object (an instance of a class).

$Clint_ip=$this->request->clintIp();

Here is an example of the code this could be using

class Example {
    public $request;

    public function __construct($request) {
        $this->request = $request;
    }

}

class Request {
    public function clintIp() {
        //return something
    }
}

And then some context:

$request = new Request;
$example = new Example($request);

$clint_ip = $example->request->clintIp();

So here, $clint_ip is the variable. $example and $request are objects (instances of classes), and clintIp() is a method of the request object.

Now, about "$this". This indicates that it is within the object "Example":

Imagine the class Example now has a method

public function test() {
    return $this->request->clintIp();
}

$this means that it is inside of an instance of an object. In static context, use "self::", as mentioned in one of the other answers.

randak
  • 1,941
  • 1
  • 12
  • 22
0

You are inside object which has request property. Request property contains object with method clintIp() which return client ip.

ishenkoyv
  • 665
  • 4
  • 9