M
M
Max Ba2018-03-07 13:16:53
PHP
Max Ba, 2018-03-07 13:16:53

How to properly inherit and connect classes?

Guys, I can't figure it out.
I have three classes.
1) User (methods: create, update, delete) in mysql for users is a separate table.
2) Organization (methods: create, update, delete) in mysql for organizations is also a separate table.
3) Invoice (accounts) (methods: create, update, delete) in mysql for accounts a separate table, but contains id_user and id_org.
It turns out that my third Invoice class will crash if I rename some column in the tables users or organizations.
Did I select the entities incorrectly, or what is the reason?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2018-03-07
@phpcoder81

Your Invoice should depend on User and Organization objects. Accordingly, inside Invoice you work with the parameters of the transferred objects.

$uid = $this->User->getId();
    $oid = $this->Organisation->getId();

They must be passed in the constructor or as separate methods. Code without comments, well, the editor is not very convenient here.
class Invoice
{
    protected $User;
    protected $Organisation;

    public function __construct(User $User = null, Organisation $Organisation = null)
    {
        if (!is_null($User)) {
            $this->setUser($User);
        }
        if (!is_null($Organisation)) {
            $this->setOrganisation($Organisation);
        }
    }
    public function setUser(User $User)
    {
        $this->User = $User;
    }
    public function setOrganisation(Organisation $Organisation)
    {
        $this->Organisation = $Organisation;
    }
}

This approach will help you get additional fields of the User and Organization objects and not worry that you will change the table.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question