-1

I see and use this syntax alot in Laravel, I wonder how it works, cause I try to integrate it in my own project but I get errors like this

FATAL ERROR Uncaught TypeError: Argument 1 passed to getVal() must be an instance of Man, none given, called in'

now here is Laravel type code as you all know

public function join(Request $request){
    echo $request['name'];
    //for instance
}

and it works fine, now here is my code:

class Man {
    public $child,
           $wife;

   public function _construct(){
       $this->child = 1;
       $this->wife = 2;
   }
}

function getVal(Man $man){
    echo $man->wife;
}

getVal();

Please help me understand this better.

samezedi
  • 621
  • 5
  • 15
  • I know it would work that way, I'm just curious how it's done this way in Laravel – samezedi Jun 12 '18 at 13:10
  • Not really about specifics man, all i'm asking is, it works in Laravel, how does it work? – samezedi Jun 12 '18 at 13:13
  • That is totally pure PHP... You call `getVal();` without pass any `Argument` as you define – Truong Dang Jun 12 '18 at 13:13
  • Are you asking about `automatic injection`? https://laravel.com/docs/5.6/container#automatic-injection. There is a lot of code behind the scenes that makes this work. It will not work automagically for you. – waterloomatt Jun 12 '18 at 13:47
  • Also notice it is `__construct()` not `_construct()`. – waterloomatt Jun 12 '18 at 13:58
  • I wouldn't know if thats the name, but from the link you provided I think that's what it is @waterloomatt – samezedi Jun 12 '18 at 13:59
  • Thanks @waterloomatt, I think people should really understand a question before voting it down – samezedi Jun 12 '18 at 14:01
  • It sounds like you want to know about how Laravel implements a `dependency injection container`. That is a large topic and I suggest you alter your question to make it a bit more specific. Here are a few links - https://stackoverflow.com/q/18562752/296555, http://php-di.org/ (never used this DI tool before but heard good things about it). – waterloomatt Jun 12 '18 at 14:40
  • Thanks I appreciate – samezedi Jun 12 '18 at 15:04

2 Answers2

1

As the error says, the value passed to getVal() must be an instance of Man, so your last line of getVal() needs to have an instance of Man passed to it.

It can be any instance of man, or itself:

$man = new Man(); getVal($man) would also work

as would:

$man = new Man(); $man2 = new Man(); getVal($man2)

This is a normal way of writing PHP code.

However, there may be something else going on in Laravel, if you could link the location in the source code, that'd be great.

bear
  • 11,364
  • 26
  • 77
  • 129
  • I know how to instantiate an object, I asked this question, because I see this stuff in Laravel where you are calling a method and then parameters are passed in such manner as I have written – samezedi Jun 12 '18 at 13:16
  • @samezedi that's a normal way of writing code, that parameters are passed into a function, which can be used. Can you link to where you see the phenomenon in Laravel? – bear Jun 12 '18 at 13:33
  • here is one example, glad I could pick this in laravel documentation https://laravel.com/docs/5.6/request when you land on that page scroll down to read some of its documentation and you will see what I am talking about – samezedi Jun 12 '18 at 13:56
  • @samezedi that link leads to a 404 – bear Jun 12 '18 at 20:30
1

Possibly wrong, but its piece of code, that could help you to find direction. Its not working, dunno how to return back to normal code.

<?php
set_exception_handler('myExcHandler');

function myExcHandler(Throwable $exception) {

    $fr = new ReflectionFunction($exception->getTrace()[0]['function']);

    $parameters = [];
    $st = new MyStaticClasses();

    foreach ($fr->getParameters() as $parameter) {
        $parameters[] = $st->getClass($parameter->name);
    }
    return call_user_func_array($exception->getTrace()[0]['function'], $parameters);
}

class Man {
    public $wife = 1;
}

class MyStaticClasses {
    public $storage;

    public function __construct() {
        $this->storage['man'] = new Man();
    }

    public function getClass($name) {
        return $this->storage[$name];
    }
}

function getVal(Man $man) {
    return $man->wife;
}

echo getVal();
Eakethet
  • 682
  • 1
  • 5
  • 18