STOP
Работали ли вы ранее с объектно-ориентированным программированием?
Использовали ли вы методы ОО моделирования?
Используете ли вы пространства имен или функциипрефиксы ("(" mylib.php "," function mylib_dosomething () {...} ")") для ваших файлов PHP?
Не переходите на UML, так быстро.UML должен сделать документацию о том, что у вас в голове.
Сначала вы должны подумать, что вы собираетесь делать с веб-сайтом, а затем - документально и смоделировать, как будет работать ваш новый веб-сайт.
UML - замечательный инструмент, но, если дизайн у вас в голове беспорядок, у вас будет беспорядок в документации.
У вас есть работающий веб-сайт, и вы хотитезамени это.Есть два основных метода.
(1) Начните с нуля:
Если у вас есть опыт работы с ОО и вы можете забыть о своем старом сайте, оставьте его работающими запустить новый веб-сайт с нуля, используя OO или некоторые из существующих фреймворков для этого.
(2) Refactor
Или вы хотитеподдержите свой текущий веб-сайт и переходите к 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();
}
?>
И ОО пока нет.Потому что это промежуточный шаг.
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();
?>
(еще не все).