我有以下问题: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