PHP OOP 核心框架 [英] PHP OOP core framework

查看:25
本文介绍了PHP OOP 核心框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是发布这个问题,所以你们中的一些人可能能够以正确的方式指出我.我在慢慢热身OOP,开始理解这个概念.我想制作一个良好的坚实核心或基础,用作 CMS 后端.它还将使用MVC.我一直在使用 http://gilbitron.github.com/PIP/ 作为 MVC 基础.

I am just posting this question so some of you might be able to point me in the right way. I am slowly warming up to OOP, starting to understand the concept. I want to make a good solid core or foundation to be used as a CMS backend. It will also use MVC. I have been using http://gilbitron.github.com/PIP/ as the MVC- base.

我想不通的是:

比如说,在后端的项目页面上,我有 2 个部分:htmltext 和项目,我应该能够同时编辑它们.uri 将类似于://域/后端/项目(该方法将是索引并显示2个部分)

Say, on the projectpage in the backend I have 2 sections: htmltext and projects and I should be able to edit them both. The uri would be something like: //domain/backend/projects (the method would be the index and show the 2 sections)

当我点击项目时应该如何处理?//域/后端/项目/项目/或//域/后端/项目/列表/

When i click on projects how should it be handled? //domain/backend/projects/projects/ or //domain/backend/projects/list/

再进一步,一个项目将包含一些图像或画廊://域/后端/项目/编辑/5/gallery/2

One step further, a project will hold some images or a gallery: //domain/backend/projects/edit/5/gallery/2

我的问题是,首先:这是否是一个好的方法,更重要的是如何在 OOP 中实现这一点

My question here is, first: would this be a good way to go and even more important how would this be implemented in OOP

主要项目控制器:

class projects {

    function index(){
        // view index
    }

    function edit{
        $project = new Project();
        $projectdata = $project->load(5);
    }
}

单个项目控制器

class project {

    function __construct(){
        $this->projectmodel = $this->loadModel('project_model'); // prepare the model to be used
    }

    function load($id){
        $this->projectmodel->loadproject($id);
    }
}

项目模型

class project_model extends model { //extends for DB  access and such

    function __construct(){
        // do stuff
    }

    function loadproject($id){
        return $this->db->query("SELECT * FROM projects where id=" . $id . " LIMIT 1");
    }
}

现在我的问题.如果这个项目有图像,我应该在哪里加载图像类来处理这些图像?我应该像 $this->images = new Images() 那样在 project_model 中加载它吗?并在模型内部有一个函数

Now my question. If this project has images, where should I load the image class to handle those? Should I load it in the project_model like $this->images = new Images(); and have a function inside the model

function loadimages($id){
    $this->images->load($id);
}

然后图像将类似于:

class image extends model { //extends for DB  access and such 

    function __construct(){
    }

    function load($id){
        return $this->db->query("SELECT * FROM project_images where id=" . $id . " LIMIT 1");
    }
}

控制器和模型似乎以这种方式混淆了.然而,从逻辑上讲,项目是一个包含项目信息的容器,项目信息可以是文本、图像,也可能是视频.我将如何在逻辑上进行设置.

It seems controllers and models gets mixed up this way. Yet in a logical way a project is a container that holds projectinfo, which could be text, images and maybe video's. How would I go about to set that up logically as well.

推荐答案

原始问题

关于 URL 的第一部分称为:路由或调度.有很好的文章关于它与 Symfony 2.x 的关系,但是其背后的理念才是最重要的.此外,您可能会查看其他框架如何实现它.

The original question

The first part, about the URLs is something called: Routing or Dispatching. There is quite good article about it in relationship with Symfony 2.x, but the the idea behind it is what matters. Also, you might looks at ways how other frameworks implement it.

至于您的原始 URL 示例,图库将存储在 DB 中.他们不会吗?他们将拥有一个唯一 ID.这使得 /backend/projects/edit/5/gallery/2 毫无意义.相反,您的网址应该看起来更像:

As for your original URL examples, galleries will be stored in DB. Won't they? And they will have a unique ID. Which makes this, /backend/projects/edit/5/gallery/2 quite pointless. Instead your URL should look more like:

/backend/gallery/5/edit         // edit gallery with ID 5
/backend/project/3              // view project with ID 3
/backend/galleries/project/4    // list galleries filtered by project with ID 4

网址应仅包含您真正需要的信息.

The URL should contain only the information you really need.

这也表示 3 个控制器:

This also would indicate 3 controllers:

  1. 单一画廊管理
  2. 单一项目管理
  3. 处理画廊列表

示例 URL 将具有与此类似的模式:

And the example URLs would have pattern similar to this:

/backend(/:controller(/:id|:page)(/:action(/:parameter)))

其中 /backend 部分是强制性的,但 controller 是可选的.如果找到控制器,则 id(或页面,当您处理列表时)和 action 是可选的.如果找到操作,则附加 parameter 是可选的.如果编写为正则表达式,这种结构可以让您处理大部分路由.

Where the /backend part is mandatory, but the controller is optional. If controller is found , then id ( or page, when you deal with lists ) and action is optional. If action is found, additional parameter is optional. This structure would let you deal with majority of your routes, if written as a regular expression.

在开始使用或编写某种 PHP 框架之前,您应该学习如何编写正确的面向对象的代码.这并不意味着知道如何编写类".意味着,您必须真正了解什么是面向对象编程,它基于什么原则,人们会犯哪些常见错误以及最普遍的误解是什么.以下是一些可能对您有所帮助的讲座:

Before you start in on using or writing some sort of PHP framework, you should learn how to write proper object oriented code. And that does not mean "know how to write a class". It means, that you have to actually understand, what is object oriented programming, what principles it is based on, what common mistakes people make and what are the most prevalent misconceptions. Here are few lecture that might help you with it:

  • Inheritance, Polymorphism, & Testing
  • Advanced OO Patterns (slides)
  • Unit Testing
  • The Principles of Agile Design
  • Global State and Singletons
  • Don't Look For Things!
  • Beyond Frameworks (slide)
  • Agility and Quality (slides)
  • Clean Code I: Arguments
  • Clean Code III: Functions

这应该会给你一些关于这个主题的概述..是的,它很多.但是怀疑您会更喜欢视频而不是书籍.否则,一些阅读材料:

This should give you some overview of the subject .. yeah, its a lot. But is suspect that you will prefer videos over books. Otherwise, some reading materials:

您会注意到很多材料都与语言无关.那是因为对于基于类的面向对象语言来说,理论是一样的.

You will notice that a lot of materials are language-agnostic. That's because the theory, for class-based object oriented languages, is the same.

注意代码中的 extends 关键字.它的意思是是一个".没关系,如果class Oak extends Tree,因为所有的橡树都是树.但是如果你有class User extends Database,有人可能会生气.实际上有一个 OOP 原则来讨论它:Liskov 替换原则 ..还有一个非常简短说明

Be careful with extends keyword in your code. It means "is a". It is OK, if class Oak extends Tree, because all oaks are trees. But if you have class User extends Database, someone might get offended. There is actually an OOP principle which talks about it: Liskov substitution principle .. also there is a very short explanation

这篇关于PHP OOP 核心框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆