M
M
Mihail2018-09-26 19:19:47
OOP
Mihail, 2018-09-26 19:19:47

How to solve the problem of returned objects?

You need to display the name of the payment client. Let's take this example:

class Payment
{
    /**
     * @var Customer|null
     */
    protected $customer;
    
    public function getCustomer()
    {
        return $this->customer;
    }
    
    public function setCustomer(Customer $customer)
    {
        $this->customer = $customer;
    }
}

class Customer
{
    public function getFirstname()
    {
        return 'Name';
    }
}

/** @var Payment $payment */
echo $payment->getCustomer()->getFirstname();

As you might guess, it happens that there is no client in the payment. Removed, for example. In this case, an error will occur because the null.
You can create a method with a check (or just a condition):
public function printCustomerFirstname(Payment $payment)
{
    if ($payment->getCustomer()) {
        echo $payment->getCustomer()->getFirstname();
    }
}

/** @var Payment $payment */
printCustomerFirstname($payment);

But how would the signer decide?
It is possible that some kind of best practice has already been developed in 2018.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
A
Arman, 2018-09-26
@Arik

1.

class Payment
{
    //...
    public function getCustomerFirstname()
    {
        return $this->customer ? $this->customer->firstname : null;
    }
}

2.
echo ($c = $payment->getCustomer()) ? $c->getFirstname() : '[удален]';

Антон Шаманов, 2018-09-26
@SilenceOfWinter

Не стоит применять слишком много "магии" или заглушек, а то в итоге станет проблематично понять где именно данных нет, а где просто пустое значение.

G
grinat, 2018-09-26
@grinat

Errors cannot be suppressed. Put try catch if it's not a critical error and catch the exception. Or check that it's not zero.

E
Evgeny Bukharev, 2018-09-26
@evgenybuckharev

ObjectHelper::getValue($payment,'customer.firstname',$defaultValue);

And inside getValue implement a property existence check, if it does not exist, then issue $defaultValue

D
dmitriy, 2018-09-26
@dmitriylanets

1.

As you might guess, it happens that there is no client in the payment. Removed, for example.

you can’t delete related entities just like that, you can mark the client as deleted, but I didn’t recommend deleting it so as not to produce a bunch of checks
2.
public function printCustomerFirstname(Payment $payment)
{
    if ($payment->getCustomer()) {
        echo $payment->getCustomer()->getFirstname();
    }
}

echo in a method in 2018 - obscene
3. Check solved by throwing an exception
public function getCustomerFirstname(): string
{
    if(!$this->getCustomer()){
       throw new \Exceptions\CustomerNotFound;
    }
    return $this->getCustomer()->getFirstname();
}

try{
$customerName = $payment->getCustomerFirstname();
}
catch( \Exceptions\CustomerNotFound $ex){
$customerName = null;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question