停止
您以前从事过面向对象编程吗?
你用过 O.O.建模技术?
您是否为您的 PHP 文件使用命名空间或函数前缀 ("("mylib.php", "function mylib_dosomething() { ... }")")?
不要跳到 U.M.L.,这么快。 U.M.L.是制作文档,
你脑子里想什么。
您必须首先考虑,您要对网站做什么,
之后,记录和模拟您的新网站将如何运作。
U.M.L.是一个很棒的工具,但是,如果你脑海中的设计是一团糟,
那么,你的文档就会一团糟。
您有一个正常工作的网站,并且想要替换它。
主要有两种方法。
(1) 从零开始:
如果您有 O.O. 方面的经验,并且可以忘记旧网站,请继续使用它,
并使用 O.O. 或一些现有的框架从头开始创建一个新网站。
(2) 重构
或者,您想维护您当前的网站,并迁移到 O.O.,
一步步。 ?它也被称为“重构”。
你可能会认为你的主程序或主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();
}
?>
目前还没有 O.O.。因为,这是一个中间步骤。
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();
?>
(更多内容)。