我有以下問題:PHP類,包括
這是我的主文件index.php
。
<?php
class myClass
{
function myClass()
{
echo '[constructor]';
$this->myVar = '[i am making a test]';
$this->A();
}
function A()
{
echo '[function A works too]';
echo $this->myVar;
}
}
$test = new myClass;
?>
現在我需要移動function A
到另一個PHP文件includes/function_a.php
,我可以include_once在主文件。原因是讓我的主文件更小,更容易閱讀。
這樣做的最佳做法是什麼?
編輯:
我做這個使用擴展:
的index.php
<?php
include_once('includes/function_a.php');
class myClass extends anotherClass
{
function myClass()
{
echo '[constructor]';
$this->myVar = '[i am making a test]';
$this->A();
}
}
$test = new myClass;
?>
包括/ function_a.php
<?php
class anotherClass
{
function A()
{
echo '[function A works too]';
echo $this->myVar;
}
}
?>
有什麼更好的想法?
所以你想'功能A'在另一個文件中,但仍然是'myClass'類的一部分? – 2012-01-31 19:52:16
爲什麼要拆分最終需要一次閱讀的內容? – Alfabravo 2012-01-31 19:55:02
是的,我希望將整個函數A放置在另一個文件中,但要具有與在主類中相同的功能。 – acoder 2012-01-31 19:59:16