here is my 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
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.
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(){
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With