dounianluo0086 2016-03-03 12:34
浏览 117

在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

  • 写回答

4条回答 默认 最新

  • drr47973 2016-03-03 14:50
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?