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
The idea is when a developer comes to a framework, he/she starts coding according to the framework perspective, and thinks how to implement the OOD methodology?
Well, the thing is you still can implement what you are trying to achieve. In your app
directory, you can create a directory, call it "classes" (or whatever you like). Start by creating your classes as usual (add the proper namespace, like in this case app\Classes
), and everything should be ok.
Make sure to add the directory to autoload
in composer.json
"autoload": {
"files": [
"App/Classes/className.php"
]
}
As it is a PHP class, add this also
"autoload": {
"psr-4": {
"MyApp\\": "app/"
},
},
And then run composer dump-autoload
. Now you can use your custom class in the controller as usual (don't forget use App\Classes\className;
). That should do it.
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.
You can do this by making your person class in core folder of codeigniter and extend it with CI_Controller and then extend person class with any class.
This is how your person class will look like:
class Person extends CI_Controller {
function __construct()
{
parent::__construct();
}
public $name='person'
public function speak()
{
echo 'person speek'
}
}
Now Save your file into codeigniter core folder. You also need to specify sub_class prefix in cofig file. Codeigniter auto loads the file which is in core folder.
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(){
}
}