Using models vs creating and binding classes
I do not understand the idea of binding classes in service provider.
I read articles about Laravel service providers and containers. I understand that Service Provider is a way to organize service objects bindings to the IoC, useful when your application is fairly large. Let's look at this case. I created 4 classes:
class Person
{
public $name;
public $surname;
public function __construct($name, $surname)
{
$this->name = $name;
$this->surname = $surname;
}
}
class Car
{
public $model;
public function __construct($model)
{
$this->model = $model;
}
}
class Adres
{
public function __construct($street, $city)
{
$this->street = $street;
$this->city = $city;
}
}
class PremiumClient
{
public function __construct(Person $p, Car $c, Adres $a)
{
$this->person = $p;
$this->car = $c;
$this->adres = $p;
}
}
As you can see I'm trying to inject three dependencies to PremiumClient class. I can achvieve it creating 4 models join them using eloquent relationship. So why is it recommended to bind classes in service provider like this:
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
Schema::defaultStringLength(191);
$this->app->bind('Car', function ($app, $p) {
return new Car($p[0]);
});
$this->app->bind('Adres', function ($app, $p) {
return new Adres($p[0], $p[1]);
});
$this->app->bind('Person', function ($app, $p) {
return new Person($p[0], $p[1]);
});
$this->app->bind('Premium', function ($app, $p) {
return new Premium(App::make('Person'),App::make('Car'),App::make('Adres'));
});
}
I'm not able to understand what are the benefits of binding classes when you can use models to achieve the same goal. When should I use models, and when create class and bind it? Please help.
laravel-5
add a comment |
I do not understand the idea of binding classes in service provider.
I read articles about Laravel service providers and containers. I understand that Service Provider is a way to organize service objects bindings to the IoC, useful when your application is fairly large. Let's look at this case. I created 4 classes:
class Person
{
public $name;
public $surname;
public function __construct($name, $surname)
{
$this->name = $name;
$this->surname = $surname;
}
}
class Car
{
public $model;
public function __construct($model)
{
$this->model = $model;
}
}
class Adres
{
public function __construct($street, $city)
{
$this->street = $street;
$this->city = $city;
}
}
class PremiumClient
{
public function __construct(Person $p, Car $c, Adres $a)
{
$this->person = $p;
$this->car = $c;
$this->adres = $p;
}
}
As you can see I'm trying to inject three dependencies to PremiumClient class. I can achvieve it creating 4 models join them using eloquent relationship. So why is it recommended to bind classes in service provider like this:
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
Schema::defaultStringLength(191);
$this->app->bind('Car', function ($app, $p) {
return new Car($p[0]);
});
$this->app->bind('Adres', function ($app, $p) {
return new Adres($p[0], $p[1]);
});
$this->app->bind('Person', function ($app, $p) {
return new Person($p[0], $p[1]);
});
$this->app->bind('Premium', function ($app, $p) {
return new Premium(App::make('Person'),App::make('Car'),App::make('Adres'));
});
}
I'm not able to understand what are the benefits of binding classes when you can use models to achieve the same goal. When should I use models, and when create class and bind it? Please help.
laravel-5
add a comment |
I do not understand the idea of binding classes in service provider.
I read articles about Laravel service providers and containers. I understand that Service Provider is a way to organize service objects bindings to the IoC, useful when your application is fairly large. Let's look at this case. I created 4 classes:
class Person
{
public $name;
public $surname;
public function __construct($name, $surname)
{
$this->name = $name;
$this->surname = $surname;
}
}
class Car
{
public $model;
public function __construct($model)
{
$this->model = $model;
}
}
class Adres
{
public function __construct($street, $city)
{
$this->street = $street;
$this->city = $city;
}
}
class PremiumClient
{
public function __construct(Person $p, Car $c, Adres $a)
{
$this->person = $p;
$this->car = $c;
$this->adres = $p;
}
}
As you can see I'm trying to inject three dependencies to PremiumClient class. I can achvieve it creating 4 models join them using eloquent relationship. So why is it recommended to bind classes in service provider like this:
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
Schema::defaultStringLength(191);
$this->app->bind('Car', function ($app, $p) {
return new Car($p[0]);
});
$this->app->bind('Adres', function ($app, $p) {
return new Adres($p[0], $p[1]);
});
$this->app->bind('Person', function ($app, $p) {
return new Person($p[0], $p[1]);
});
$this->app->bind('Premium', function ($app, $p) {
return new Premium(App::make('Person'),App::make('Car'),App::make('Adres'));
});
}
I'm not able to understand what are the benefits of binding classes when you can use models to achieve the same goal. When should I use models, and when create class and bind it? Please help.
laravel-5
I do not understand the idea of binding classes in service provider.
I read articles about Laravel service providers and containers. I understand that Service Provider is a way to organize service objects bindings to the IoC, useful when your application is fairly large. Let's look at this case. I created 4 classes:
class Person
{
public $name;
public $surname;
public function __construct($name, $surname)
{
$this->name = $name;
$this->surname = $surname;
}
}
class Car
{
public $model;
public function __construct($model)
{
$this->model = $model;
}
}
class Adres
{
public function __construct($street, $city)
{
$this->street = $street;
$this->city = $city;
}
}
class PremiumClient
{
public function __construct(Person $p, Car $c, Adres $a)
{
$this->person = $p;
$this->car = $c;
$this->adres = $p;
}
}
As you can see I'm trying to inject three dependencies to PremiumClient class. I can achvieve it creating 4 models join them using eloquent relationship. So why is it recommended to bind classes in service provider like this:
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
Schema::defaultStringLength(191);
$this->app->bind('Car', function ($app, $p) {
return new Car($p[0]);
});
$this->app->bind('Adres', function ($app, $p) {
return new Adres($p[0], $p[1]);
});
$this->app->bind('Person', function ($app, $p) {
return new Person($p[0], $p[1]);
});
$this->app->bind('Premium', function ($app, $p) {
return new Premium(App::make('Person'),App::make('Car'),App::make('Adres'));
});
}
I'm not able to understand what are the benefits of binding classes when you can use models to achieve the same goal. When should I use models, and when create class and bind it? Please help.
laravel-5
laravel-5
asked Jan 3 at 18:14
appsonappson
609
609
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54027651%2fusing-models-vs-creating-and-binding-classes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54027651%2fusing-models-vs-creating-and-binding-classes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown