为什么我不能将一个类划分为多个文件 [英] Why I can't divide a class over several files
问题描述
我试图创建一个 TestClass
分成几个文件。我已经拆分了3个文件,其中第一个文件 TestClassPart1.php
有类的开始 class TestClass {
最后一个文件 TestClassPart3.php
有类的结束括号。这些是3个文件
// TestClassPart1.php
<?php
class TestClass {
public function func1(){
echofunc 1;
}
//TestClassPart2.php
<?php
public function func2(){echofunc 2; }
//TestClassPart3.php
<?php
public function func3(){echofunc 3; }
}
然后我在实际的类文件c $ c> TestClass.php 所以TestClass.php只是所有3个文件的粘合。
<?php
require'TestClassPart1.php';
require'TestClassPart2.php';
require'TestClassPart3.php';
我认为这应该可以工作,但是当我尝试创建一个 TestClass
并调用其中一个函数,我得到 parse错误,期待T_FUNCTION'在C:\wamp\www\TestClassPart1.php第5行
。第5行是 func1()
<$ p的
$ p> <?php
require'TestClass.php';
$ nc = new TestClass()?
$ nc-> func1();
这应该不行吗?我想你可以传播一个类在几个文件没有问题。
解决方案当您 require
您的类不完整,所以当PHP解析
class TestClass {
public function func1(){
echofunc 1;
}
它不能理解类,因为关闭}
并预期你的下一个问题。这
class Foo
{
include'methods.php'
}
也不起作用。
从 PHP手册在OOP 4 (无法在5中找到)
转换为多个文件。你也不能将类定义分成多个PHP块,除非断点在方法声明内。以下不工作:
<?php
class test {
?>
<?php
function test(){
print'OK';
}
}
?>
但是,以下是允许的:
<?php
class test {
function test(){
? >
<?php
print'OK';
}
}
?>
如果您正在查找水平重用,请等待PHP.next,包括Traits 或查看
I'm trying to create a class TestClass
that's divided over several files. I have split it over 3 files where the first file TestClassPart1.php
has the start of the class class TestClass {
and the last file TestClassPart3.php
has the closing bracket of the class. These are the 3 files
//TestClassPart1.php
<?php
class TestClass {
public function func1(){
echo "func 1";
}
//TestClassPart2.php
<?php
public function func2(){ echo "func 2"; }
//TestClassPart3.php
<?php
public function func3(){ echo "func 3"; }
}
I then recombine in the actual class file called TestClass.php
so TestClass.php is just the glue of all 3 files.
<?php
require 'TestClassPart1.php';
require 'TestClassPart2.php';
require 'TestClassPart3.php';
I thought this should work, but when I try to create an instance of TestClass
and call one of the functions, I get parse error, expecting T_FUNCTION' in C:\wamp\www\TestClassPart1.php on line 5
. Line 5 is the }
of func1()
<?php
require 'TestClass.php';
$nc = new TestClass();
$nc->func1();
Shouldn't this work? I thought you could spread a class over several files no problem. Am I doing it wrong?
解决方案 When you require
a file, PHP will parse and evaluate the contents.
You class is incomplete, so when PHP parses
class TestClass {
public function func1(){
echo "func 1";
}
it's not able to make sense of the class, because the closing } is missing.
Simple as that.
And to anticipate your next question. This
class Foo
{
include 'methods.php'
}
will not work either.
From the PHP Manual on OOP 4 (couldnt find it in 5)
You can NOT break up a class definition into multiple files. You also can NOT break a class definition into multiple PHP blocks, unless the break is within a method declaration. The following will not work:
<?php
class test {
?>
<?php
function test() {
print 'OK';
}
}
?>
However, the following is allowed:
<?php
class test {
function test() {
?>
<?php
print 'OK';
}
}
?>
If you are looking for Horizontal Reuse, either wait for PHP.next, which will include Traits or have a look at
这篇关于为什么我不能将一个类划分为多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!