Support the ongoing development of Laravel.io →
Architecture
Last updated 2 years ago.
0

Since u dont have objects as a dependency, app::make would not make any difference, it would be to resolve or instantiate dependencies (depedency injection)

U have only a string, to be a query term

What version of laravel are you on?

Specification pattern is a class to encapsulate business rules, what are u trying to achieve on such term?

http://culttt.com/2014/08/25/implementing-specification-pattern/

May i see your specification class?

Sorry typing im on mobile...

0

My specification class implements an interface that requires the class to create a toSql function, I did this, since specifications can actually be represented by different kind of queries and I don't want to add a ton of findBy[enter condition here] functions in my repository class. The issue that concerned me in my code above is that I won't be able to mock the specification class above since I used the new keyword within the class. So I thought if I use App::make since its a facade I now have a way to mock it but of course I wanted to make sure that I'm coding it the best way possible.

0

Hi, I have read your comment and perhaps what you where looking for is for named constructors. Every time you want to avoid the use of the new keyword and give semantic meaning to your code you may use this named constructors. They are known as Semantic Constructors or Constructor Methods also. What you are doing is to define a static method that returns the an instance of the object itself. At the end what you have is a static factory method.

I provide you an expample from: http://verraes.net/2014/06/named-constructors-in-php/

Without named constructors you could have the following:

final class Time
{
    private $hours, $minutes;
    public function __construct($timeOrHours, $minutes = null)
    {
        if(is_string($timeOrHours) && is_null($minutes)) {
            list($this->hours, $this->minutes) = explode($timeOrHours, ':', 2);
        } else {
            $this->hours = $timeOrHours;
            $this->minutes = $minutes;
        }
    }
}

With named constructors:

final class Time
{
    private $hours, $minutes;

    public function __construct($hours, $minutes)
    {
        $this->hours = (int) $hours;
        $this->minutes = (int) $minutes;
    }

    public static function fromString($time)
    {
        list($hours, $minutes) = explode($time, ':', 2);
        return new Time($hours, $minutes);
    }

    public static function fromMinutesSinceMidnight($minutesSinceMidnight)
    {
        $hours = floor($minutesSinceMidnight / 60);
        $minutes = $minutesSinceMidnight % 60;
        return new Time($hours, $minutes);
    }
}
Last updated 6 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.