Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I can't divide a class over several files

Tags:

oop

php

class

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.phpso 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?

like image 789
park Avatar asked Dec 29 '22 03:12

park


2 Answers

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

  • Can I include code into a PHP class?
like image 165
Gordon Avatar answered Jan 11 '23 03:01

Gordon


I had this same thought a while back as a purely academic interest. It's not directly possible to do what you're asking, although you are able to use PHP to produce PHP that then gets evaluated by the server.

Long story short:

Don't bother

Short story long:

  • it adds a level of insecurity to your classing system as it becomes harder to control file access.
  • it slows down the compilation/caching of pages
  • you really don't need to force a square peg into a round hole.

Instead: use proper OOP practices of separating functionality into classes and extending existing classes.

like image 20
zzzzBov Avatar answered Jan 11 '23 01:01

zzzzBov