停止
您以前使用过面向对象编程吗?
您是否使用过 OO 建模技术?
您是否为您的 PHP 文件使用命名空间或函数前缀 ("("mylib.php", "function mylib_dosomething() { ... }")")?
不要跳到UML,这么快。UML 是为您的头脑中的内容制作文档。
您必须首先考虑您将如何处理网站,然后再记录和建模您的新网站将如何运作。
UML 是一个很棒的工具,但是,如果您脑海中的设计是一团糟,那么您的文档就会一团糟。
您有一个工作网站,并想替换它。主要有两种方法。
(1) 从零开始:
如果您有使用 OO 的经验,并且您可以忘记您的旧网站,让它继续工作,然后使用 OO 或一些现有的框架从“零”开始一个新的网站。
(2) 重构
或者,您想维护您当前的网站,并逐步迁移到 OO。? 它也被称为“重构”。
一开始你可能会认为你的主程序或主 php 文件是一个大对象(程序)文件,每个库文件也是对象。
例子:
假设您有几个 php 文件。有些文件是页面的主要文件。一些文件被包含甚至在页面文件中重复。其他的是只有功能的“库”文件。
<?php
// "indexfuncs1.php"
// this is an auxiliary file for "index.php",
// and has some free procedural code.
echo "indexfuncs1.php: dosomething()";
?>
<?php
// "funcslib.php"
// this is an library file,
// and has only functions and constants,
define ("ANYCONST", "Hello World");
function HelloWorld()
{
echo ANYCONST;
}
?>
<?php
// "index.php"
// only declarations, doesn't do anything, by itself
//include("funcslib.php");
//require("funcslib.php");
//require_once("funcslib.php");
include_once("funcslib.php");
// this code is in other file, and its executed
include("indexfuncs1.php");
echo "index.php: Hello World";
HelloWorld();
// this code is in other file, and its executed
include("indexfuncs1.php");
?>
并开始将它们变成这样:
<?php
// "indexfuncs1.php"
// this is an auxiliary file for "index.php",
// and has some free procedural code.
function indexfuncs1_dosomething()
{
echo "indexfuncs1.php: dosomething()";
}
?>
<?php
// "funcslib.php"
// this is an library file,
// and has only functions and constants,
define ("funcslib_ANYCONST", "Hello World");
function funcslib_HelloWorld()
{
echo funcslib_ANYCONST;
}
?>
<?php
// "index.php"
// only declarations, doesn't do anything, by itself
//include("funcslib.php");
//require("funcslib.php");
//require_once("funcslib.php");
include_once("funcslib.php");
function index_main()
{
// this code is in other file, and its executed
indexfuncs1_dosomething();
echo "index.php: Hello World";
funcslib_HelloWorld();
// this code is in other file, and its executed
indexfuncs1_dosomething();
}
?>
而且还没有 OO。因为,它是一个中间步骤。
Lets start by transform each web page into a single class, without inheritance, without parent classes.
<?php
// "indexfuncs1.php"
// this is an auxiliary file for "index.php",
// and the free procedural code have become a class.
class indexfuncs1 {
function dosomething()
{
echo "indexfuncs1.php: dosomething()";
} // function dosomething()
} // class IndexPage
?>
<?php
// "index.php"
// only declarations, doesn't do anything, by itself
//include("funcslib.php");
//require("funcslib.php");
//require_once("funcslib.php");
include_once("funcslib.php");
class IndexPage {
function main()
{
$myAux = new indexfuncs1();
// this code is in other file, and its executed
$myAux->dosomething();
echo "index.php: Hello World";
funcslib_HelloWorld();
// this code is in other file, and its executed
$myAux->dosomething();
} // function main()
} // class IndexPage
function index_main()
{
$myPage = new IndexPage();
$myPage->main();
} // function index_main(...)
// --> the only allowed global procedural code:
index_main();
?>
(更多来)。