Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

implement object oriented design in laravel

Tags:

php

laravel

here is my class diagram

class diagram

implementation of the classes are show in below

person class

class Person
{
    public $name='person'

    public function speak()
    {
        echo 'person speek'
    }
}

student class

class Student Extends Person
{
    public $studentNumber;

    public function learn()
    {
        echo 'learn';
    }
}

Professor class

class Professor Extends Person
{
    public $salary;

    public function teach()
    {
        echo 'teach';
    }
}

i want to implement these classes in laravel

controllers in mvc pattern frameworks like laravel,codeigniter are extends from base controller therefore in those frameworks cannot create controllers for each class and inherit that from parent class?

it is the problem i'm having

like image 426
Sachithrra Dias Avatar asked Mar 03 '16 12:03

Sachithrra Dias


2 Answers

You seem little confused there, might want to study little about how applications are coded with MVC architecture.

Now to the answer, you have not mentioned if you want to persist these classes to database or not. If you don't want to save them to database then see the above answer. Define you classes where you want, and use them like you would in any other language.

On the other hand you might want to persist these classes to database, then you would like to define these classes as Models. Create a Person base Model and extend the others from there.

You don't need to create separate controllers for each, just create as many you need. You can use one or all of the above models from one or more controllers.

like image 81
mfahadi Avatar answered Oct 20 '22 22:10

mfahadi


It depends on how you want to go about it.

You can simply define a trait with the common stuff and use it just like how you would do with a Class.

Also you will have to deal with name-spacing as well if it gets complicated.

http://php.net/manual/en/language.oop5.traits.php

trait PersonTrait {
    public $name ='Mr. Awesome';

    public function speak(){

    }
}

class Student {
    use PersonTrait;

    public function learn(){

    }
}

class Professor {
    use PersonTrait;

    public function teach(){

    }         
}
like image 34
Haneez Avatar answered Oct 20 '22 22:10

Haneez