Request $request in object-oriented PHP

Asked

Viewed 165 times

1

I’m doing some PHP OO exercises and came across some functions that call a class before the variable. But I didn’t really understand why.

For example:

public function Exemplo(Request $request, $nome, $sobrenome) {
   // métodos
}

if I call a class to use as parameter because comma is not used to stop the class of the variable ?

It must be something simple, but it’s causing confusion here. I’d appreciate it if you could enlighten me

hug.

  • It is not parameter, there is being warned that $request is "type" Request (can not for an integer, a string or something, has to be an object of type Request)

  • I am still confused. Then the $request variable will be treated as a Request ? class heretic inheriting all its attributes and methods ?

  • This means if the variable that the Laravel should enter must be a Request object. Ex: $request = new Request(); $controller = new Controller(); $controller->Exemplo($request);. Like Bacco said, it can’t be $controller->Exemplo("string"); or $controller->Exemplo(123456).

  • https://secure.php.net/manual/en/functions.arguments.php#example-154

  • Hmm. Let’s assume I create a class like this: class Soma { public function Somar(){ return 25 + 25; } } --- So I want to call this function on another leaf doing so: public function SomarTotal ( Somar $somar ) {} Then when I call the function I must inform an object of type Somar, right ? like this: $a = new SomarTotal; $a->Somar(); Is that it ??? Vish ta fuck assimilate it

  • It’s not that complex. The name of it is Types Hinting. http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration

  • Oops, good brother. The name is already worth a lot. I will study in depth here.. vlw

Show 2 more comments

1 answer

0


There is no typing in php at first... But in this case, the variable $request has a type yes, the type Request.

Now, imagine with us, you make a code, where you assign a int to the variable $request by mistake... You would lose all information from $request: data, cookies, tokens...

"Typing" the variable $request you make sure that no unnecessary assignments are made on it, and accidental loss of variables.

PS.: You are not using a class as a parameter, you are setting that $request is the type Request, as in typed languages you do with other variables such as int numero;, double numero, String texto...

I hope I’ve helped!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.