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?