0

I have recently begun building webpages using PHP, and being inexperienced, I have a question about coding conventions. Is it considered bad style to use require to dynamically load page content or page elements in a web page?

For example, if you are modularizing your code, and have a /resources/pageContent/ folder with different files containing the content of different pages. Would it be considered bad style to say require ("resources/pageContent/profile.php")? Another way I've seen this done is using fopen(), but of course, it won't allow you do use any PHP code in your dynamically loaded pages and will only print the HTML and CSS.

I am just trying to put things into modules, since putting things in functions (For example, loadPageProfile) can get really messy-looking.

4

2 回答 2

2

首先,如果您想编写干净且可维护的代码,请不要使用错误抑制器@ 聆听所有错误 - 大小错误。这将对您有很大帮助。真的。

error_reporting(E_ALL);

完成后,只需更改

error_reporting(0);

我只是想把东西放到模块中,因为把东西放到函数中(例如,loadPageProfile)看起来真的很乱。

好吧,那么您就走上了正确的道路...您所要做的就是创建一个名为的模块,例如Page. (我们现在正在谈论页面)

使用过程代码来创建模块是不好的做法,因为我们正在讨论它们。

只需将所有逻辑封装到一个类中,如下所示:

文件Page.php

abstract class Page {

   /**
    * Includes chunk of the page
    * it's Useful, when you have number of pages
    * and want for example only one chunk to be displayed
    * everywhere
    * This could be footer or menu or something like this "static" parts
    * 
    * @param string $block
    * @return void
    */
   public static function useBlock($block)
   {  
      $file = 'path_to_blocks' . DIRECTORY_SEPARATOR . $block; 

      //Ensure this is valid stream before we include it;
      if ( is_file($file) ){
         // No need to use require() here
         // Because we are sure that file exists and valid for inclusion
         // include is a bit faster that require()
         include($file);
      }
   }

   /**
    * Displays some page
    * This is just simply form of require, but
    * this method would simplify inclusion 
    * 
    * @param string $page
    * @return void
    */
   public static function DisplayPage($page)
   {
     $file = 'path_to_your_pages' . DIRECTORY_SEPARATOR . $page;

     if ( is_file($file) ){
        include($file); 
     }
   }

}

现在假设你有 pages: contact, index, profile, login,register所以不用require()到处使用你只需调用那个“舒适”的方法。

同时,页脚和菜单可能与此类似:

文件:footer.phtml

<div id="footer">Copyrigth (c) you and bla bla bla</div>

文件:menu.phtml

<li><a href="/">Home</li>
<li><a href="/register/">Register</a></li>
<li><a href="/contact/">Contact</li>

要要求特定的类,您可以创建一些module,比如这个:

class Import {

   /**
    * 
    * @param string $class Class File name to be required
    * @param string $ext filename extension (just to simplify )
    * @return bool
    */
   public static function getSomeClass($class, $ext = '.php'){

      $location = 'folder_of_classes' . DIRECTORY_SEPARATOR . $class . $ext;
      return spl_autoload_register(function() use ($location){
         // We won't use include() here
         // Because we'd to stop (producing fatal error) if inclusion would fail
         require_once ($location);
      });
   }

}

然后,当您需要特定课程时,只需致电

<?php
// requires MySQL_PDO.php located in defined foldet
Import::getSomeClass('MySQL_PDO');

请记住,当我们谈论模块时,在 99% 的情况下,我们谈论的是在这个模块中实现的类。

另一个建议是:

1) 不要将 CSS 与 HTML 混合(创建单独的 css 文件并将其包含在特定页面上,通过<link href="path_to_css.css" rel="stylesheet" type="text/css" />

因为它使标记清晰且易于将来维护(例如,当您想更改样式或添加某些内容时)

2) 不要混用 PHP 和 JavaScript 代码。将 JavaScript 文件和 CSS 保存在单独的文件中。使用 Ajax 在 PHP 和 JavaScript 之间共享变量

3) 不要将它们全部混合 HTML、CSS、JavaScript 和 PHP。尤其是 HTML。将其modules(类或业务逻辑)保存在单独的文件中。然后只包括特定任务所需的部分。

于 2012-11-11T11:00:10.017 回答
1

不,这不是糟糕的风格,它实际上是一种很好的做法。继续使用它。

于 2012-11-11T09:48:22.450 回答