Problems passing instantiated object by function parameter with php

Asked

Viewed 2,042 times

1

I have a file php that instance a class, however I am not able to pass this instantiated object as parameter to another class.

Example:

$usuario = new Usuario();
$email = new Email();
$email->send($usuario);

In class Email I can’t use the class getters and setters user.

class Email {
    function send($usuario) {
        mail($usuario->getId());
    }
}
  • 1

    Enter the code of Email::send also

  • Ready, I put.

  • 1

    But the function has no name? function($usuario) {

  • Whoa, I forgot to put it on, it’s called send.

  • mail() is that default php function? $usuario->getId() returns some error?

  • Well, in vdd this is just an example. My code is much more complex than that. But even if I put return $usuario->getId() it doesn’t work

  • 2

    @Krint then edit the question and correct. Put at least this method in full.

Show 2 more comments

2 answers

1

in the email class in your send method before the parameter specifies the obj q will pass type

public function send(Usuario $usuario){
// seu codigo
}

I hope that’s what you’re looking for

  • I need to declare the User object?

  • 2

    Not required. This only limits the type of parameter the method accepts, as for any object of another type an error TypeError would be launched. But if you do not specify, no problem. Probably this is not the solution.

1


The problem you’re having is Dependency injection .

The dependency of one class occurs when "injected", passes one class to be worked within the other.

This means that in order to have an injection of dependency, the instantiation of an object must not take place within the class, but outside it and then injected.

One of the ways to accomplish this is by injecting the external class through the construction method.

Let’s take a look at the example:

<?php

class Email  
{
    public function send()
    {
        // TODO
    }
}

class Usuario  
{
    protected $email;

    //Injeção de dependência através do método construtor
    public function __construct(Email $email)
    {
        $this->email = $email;
    }

    public function porEmail()
    {
        $this->email->send();
    }
}

__Construct() is how to declare a constructor method in PHP, different from languages like Java where the same class name is used, for example: public Usuario().

Note that now when calling the method $command->porEmail() we’re actually triggering the method send() class $email , for example, we can do so:

$email = new email();

$anunciar = new Usuario($email);
$anunciar->porEmail();

Note that the class Usuario receives by addiction injection $email. This whole process that we’ve done means dependency injection. You see how simple it is?

  • 3

    His explanation of addiction injection is good, but is that really his problem? I’m not sure from what he said so far.

  • I changed the example to make it clearer and more appropriate to his problem. I believe that’s what he wants to do.

  • But this he is apparently already doing, just not specifying the type of parameter in the constructor. But if you take that out, it works perfectly (http://ideone.com/bOgTHR). That’s why we’re talking about that’s probably not the problem.

  • 1

    Actually this was quite the problem. When I used this way @Dnick says, it worked perfectly

Browser other questions tagged

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