Welcome Guest, Not a member yet? Register   Sign In
Application logic inside class or inside model?
#1

[eluser]andyy[/eluser]
If one was writing an application that has all the functions broken down into class methods and you need to run some logic: if, else, etc - should you have the methods returning bool values and create the logic inside of the model, or should you have the class/method run the logic returning the result? It's hard to explain, hopefully that makes sense. Maybe an example to help:

How I was thinking it should work..
Code:
/**
* This would be the model
*
*/
$z = new Payments();
$z->method     = 'credit_card';
$z->cardNumber = 1111222233334444;
$z->cardExpiry = 0114;
$result = $z->chargeCard();

if($result['status'] == 'success')
{
    echo $result['message'];
    // more logic pertaining to a successful payment..
}
else
{
    echo $result['message'];
    // more logic pertaining to an failed payment..
}

But I've also thought about this..
Code:
/**
* This would be the model
*
*/
$z = new Payments();
$z->method     = 'credit_card';
$z->cardNumber = 1111222233334444;
$z->cardExpiry = 0114;
$z->chargeCard();

/**
* This would be the Payments class
*
*/
public function chargeCard()
{
    // Logic to charge the credit card here...
    // ......................................
    // ......................................

    if($result_of_transaction == false)
    {
        echo 'Payment failed';
        // more logic pertaining to an failed payment..
    }
    else
    {
        echo 'Payment complete';
        // more logic pertaining to an successful payment..
    }
}

I've been thinking that any method's result should be determined within the model. If there is anything more than a bool value that needs to be returned, use an array and use the arrays values within the model.

Thoughts?
#2

[eluser]bscott[/eluser]
Or you could have getStatus and getErrorMessage methods in your Payment class and use

Code:
$z->chargeCard();

if($z->getStatus() == 'success')
{
    echo 'Payment complete';
    // further logic pertaining to a successful payment..
}
else
{
    echo $z->getErrorMessage();
    // further logic pertaining to an failed payment..
}

This way the determination of sucsess/failure and the content of the error is encapsulated in the Payment class and the action taken dependant on the result of the chargeCard method is in the model.




Theme © iAndrew 2016 - Forum software by © MyBB