PHP应用程序结构

发布于 2024-11-18 20:08:45 字数 1128 浏览 4 评论 0原文

我开始制作一个网站,很快我发现我的代码一团糟。我已经有一段时间没有使用 PHP 编程了,从那时起我学习了 OOP。现在,虽然从这个角度来看,用 C# 或 java 制作应用程序非常容易,但 PHP 却给我带来了麻烦。

我曾经用最少量的方法(在 php 中)从一行到另一行对所有内容进行编程,并且根本没有类。 那么方法中应该有多少内容,这些类应该在哪里?

示例: 我有 account.php,我想更新用户信息。因此,在检查有人发送了一些数据后 if(isset($_POST[..) 接下来做什么?我过去只是做 所有检查,如 $email=CheckForRisksOrSomething($_POST["email]);,然后更新 mysql。

这给我带来了一个问题,我应该创建一个类User< /em> 它应该包含哪些方法? 另外,假设 user.php 会显示用户的个人资料,那么这个类应该保存

在 哪里?我只是用在这样的文件中:

$smth = new Someclass();
if($smth->checkIfSaved()){
    $user = new User();
    $user->updateUser(/* all the $_POST stuff here? */); 
} else {
    $smth->showUserDetailsForm();
}

另外,Someclass 应该保存在哪里?

所以如果有人能给我一个如何构建以下功能的示例(在文件、类和可能的

  • 用户 中)。编辑帐户..)
  • 带有评论的图片
  • 新闻(也带有评论)
  • 管理

这当然只是一个例子,但我不想继续我的混乱,然后三天后再看它,只是捂脸和疑惑我在那里写了什么,我已经写了。今天,这就是我问的原因


编辑 24.2.2016

虽然这是一个老问题,但建议研究一下框架。虽然设置它可能需要一些时间,但它提高了开发速度,代码漂亮、干净,而且更安全。我个人使用 Laravel。

I started making a website, and quickly i found out that my code is a mess. It's been a while since I've been programming in PHP, and since then I learned OOP. Now while making an application in C# or java is pretty easy from this point of view, PHP is giving me trouble.

I used to program everything (in php) just from one line to another, with minimum amount of methods and no classes at all. So how much should be in methods, and where should this classes be?

Example:
I have account.php, and i want to update user information. So after checking that someone has sent some data if(isset($_POST[.. what next? I used to just make
all the checks like $email=CheckForRisksOrSomething($_POST["email]); and then just update mysql.

This brings me to the question, should I create a class User and what methods should it contain? Also WHERE should this class be saved, assuming that user.php would show you user's profile.

Back to how much should be in classes, should i just use in a file like:

$smth = new Someclass();
if($smth->checkIfSaved()){
    $user = new User();
    $user->updateUser(/* all the $_POST stuff here? */); 
} else {
    $smth->showUserDetailsForm();
}

Also where should Someclass be saved?

So if ayone would give me an example of how to structure the following functionalities (in files, classes and possibly methods).

  • users (registration, login, editing account..)
  • picture with comments of it
  • news (also with comments)
  • administration

This is just an example of course, but i don't want to keep on with my mess and then look at it three days later and just facepalm and wonder what did i write there. Well, i already did that today, that's why I'm asking.


EDIT 24.2.2016

While this is an old question, as suggested look into a framework. While it might take a bit to set it up it increases development speed, code is nice and clean and a lot safer. I personally use Laravel.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

醉态萌生 2024-11-25 20:08:45

给这只猫剥皮的方法不止一种,但我会为您推荐一些有用的链接和书籍。

Zend 的编码标准:如果您正在研究如何构建,请命名等你的文件,然后我会看看 Zend 建议如何做这些事情。通过遵循他们的指南,您也可以使用这些相同的方法创建与其他人的代码兼容的包。

您需要做的是专门为您的课程创建一个文件夹。您如何知道何时需要创建新课程以及包含哪些内容?好问题!

如果您发现自己遇到了一组相关的信息,需要经常一起处理,或者解决一个特定的问题,那么您很可能应该创建一个可以处理它的新类。

例如,一个 User 类,用于处理您想要对站点用户进行的任何类型的更改。另一个示例是角色类,它可以执行(或不执行)任务,具体取决于他们是否是管理员。对象和类的美妙之处在于您可以创建一个 User::$role(User 类中名为 $role 的变量),它是一个 Role 类。例如,您的 User 对象中将有一个 Role 对象。从这里开始,您的用户对象现在可以通过执行以下操作来调用角色对象:

class User {
    private $username;
    private $role;

    function __construct($username){
        $this->username = $username;
    }
    public function setRole($roleType){
        switch($roleType){
            case "admin":
                $this->role = new Admin(); // contained in another class file
                break;
            case "staff":
                $this->role = new Staff(); // contained in another class file
                break;
            default:
                $this->role = new Visitor(); // contained in another class file
            } // end case
        } // end setRole
} // end Class User

$loggedInUser = new User("Bubba");
$loggedInUser->setRole("admin");
$loggedInUser->role->deleteMessage();

一般来说,您将希望保持代码的结构化和独立性,以便每个类仅处理它应该具有的信息,并且不做任何超出的事情它应该。如果您发现一个类做了太多事情,这意味着您找到了创建另一个类来处理这个新职责的好时机。如何将它们结合在一起通常是一个用“设计模式”来回答的问题。有一本非常好的书,涵盖了使用对象和类进行编程以及使用设计模式。

查看 Matt Zandstra 所著、 Apress 出版的《PHP 5 对象、模式和实践》一书。这是一本精彩的书,让您深入了解 PHP 的 OOP!

There's more than one way to skin this cat, but I'll recommend some helpful links and books for you.

Zend's Coding Standards: If you're looking at how to structure, name, etc. your files, then I would look at how Zend suggests to do these things. By following their guidelines, you can create a package that is compatible with somebody else's code using these same methods, as well.

What you will want to do is create a folder specifically for your classes. How do you know when you'll need to make a new class, and what to include? Good question!

If you find yourself running into groups of information that is related, needs to be handled together a lot, or solves a particular problem, chances are you should create a new class that can handle it.

For example, a User class to handle any sort of changes you want to make to a user of your site. Another example is a Role class that can perform tasks (or not) depending on if they're an admin or not. The wonderful thing about Objects and Classes is you can create a User::$role (variable named $role in the User class) that is a Role class. So for instance, you will have a Role object in your User object. From here, your user object can now call to the role object by doing something like:

class User {
    private $username;
    private $role;

    function __construct($username){
        $this->username = $username;
    }
    public function setRole($roleType){
        switch($roleType){
            case "admin":
                $this->role = new Admin(); // contained in another class file
                break;
            case "staff":
                $this->role = new Staff(); // contained in another class file
                break;
            default:
                $this->role = new Visitor(); // contained in another class file
            } // end case
        } // end setRole
} // end Class User

$loggedInUser = new User("Bubba");
$loggedInUser->setRole("admin");
$loggedInUser->role->deleteMessage();

In general, you will want to keep your code structured and separate, so that each class only handles information that it should have, and not do anything more than it should. If you find a class doing too many things, this means you found a good time to create another class to handle this new responsibility. How you tie it all together is generally a problem answered with "design patterns". There is a very good book that covers both programming using objects and classes, as well as using design patterns.

Check out the book "PHP 5 Objects, Patterns, and Practice" by Matt Zandstra, and published by Apress. It's a wonderful book that dips your toes into OOP with PHP!

拥抱影子 2024-11-25 20:08:45

很难告诉你应该如何做。有许多不同的偏好。查看 ZendKohanaCodeIgniter 等。

当我使用 OOP 设计创建较小的应用程序时,我通常尝试遵循以下层次结构:

  • index.php 中包含 bootstrap.php
  • bootstrap.php 中包含所有类和配置。
  • 创建一个 classes 目录,您可以在其中的 core 文件夹中保存核心功能。
  • 此外,在 classes 目录中创建两个文件夹 controllersmodels
  • 在我的项目中,我通常有另一个文件夹 templates,其中包含我所有的 html 文件(然后使用 Smarty 模板引擎进行渲染)。

这是一种非常简化的方法,但可以引导您走向正确的方向。根据您的需求(例如用户、图片等),创建一个包含单个用户的所有数据的模型。

class UserModel() {
    public $name;
    public $email;
}

然后是一个可以修改这个模型的控制器:

class UserController() {
    public function update($user) {
        ...
    }
}

It is very hard to tell you how you should do that. There are many different preferences out there. Look at frameworks like Zend, Kohana, CodeIgniter, and so forth.

When I create smaller applications with OOP design, I usually try to follow the following hierarchy:

  • In the index.php include a bootstrap.php.
  • In the bootstrap.php include all classes and the config.
  • Make a classes directory where you can save core functionallity in a core folder.
  • Furthermore, in your classes directory create the two folders controllers and models.
  • In my projects I usually have another folder templates, where all my html files go in (and afterwards get rendered with the Smarty Template Engine).

This is a very simplified approach, but can lead you in the right direction. For your requirements like users, pictures, etc. create a model that contains all the data of one single user.

class UserModel() {
    public $name;
    public $email;
}

And then a controller that can modify this model:

class UserController() {
    public function update($user) {
        ...
    }
}
み格子的夏天 2024-11-25 20:08:45

我的回答可能不会直接回答您的问题,但可能对您有很多好处。

使用真正的框架。不要从头开始使用代码构建网站。它将节省您大量的时间,并迫使您使用代码分离的良好实践,真正良好且干净的项目结构,但同样,它会节省您的时间!对于您决定实施的许多事情,那里都会有一个现成的解决方案,并且您所做的常见事情(如验证电子邮件地址)将以正确的方式完成。

我会选择 MVC PHP 框架,在过去使用过很多 CakePHP、Zend、Kohana 和其他一些框架之后,我建议您选择 Yii。非常小的学习曲线,不错的文档,不错的大型活跃社区,您将能够在很短的时间内将您已有的任何内容移植到其中。

我非常确定,对于您描述的项目,该框架将减少您至少 2 倍的时间,同时您上面提出的所有问题都将得到解决。我从经验中知道这一点,很久以前我就从头开始开发过项目。

My answer might not be directly answering your questions but might do you lot of good.

Go with a real framework. Don't build websites with code from scratch. It will save you huge amount of time and will force you to use good practices of code separation, really good and clean project structure but again, it will save you time! For many things you decide to implement there there will be a ready solution and the common thing you do (as validate email address) would be done the right way.

I would go with a MVC PHP framework and after having used a lot CakePHP, Zend, Kohana and few others in the past, I would recommend you to go with Yii. Very small learning curve, nice docs, nice big active community and you will be able to port whatever you already have, to it in very small time.

I'm pretty sure for the project you've described that framework will cut your time at least 2 times and in the same time you will have all the things you ask above already solved. I know this from experience, having developed projects from scratch quite some time ago.

腹黑女流氓 2024-11-25 20:08:45

如果您正在开发 Web,那么您很有可能会开发 MVC 应用程序,我所知道的最简单的 MVC 框架是 CodeIgniter。我会建议你使用它。

if you are developing for web, there are high chances that you will be developing a MVC application, simplest MVC framework known to me is CodeIgniter. And I will advice you to use it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文