1

how insert method in class from another file? Now i get error:

`T_FUNCTION' in C:\wamp\www\index.php on line 9

    index.php file:
    <?php

    class cars
    {
        public function go()
        {
            echo 'go go';
        }    
        include('stop.php');
    }

    $c = new cars;
    $c->go();
    ?>

    stop.php file
    <?php
    public function stop()
    {
        echo 'stop method';
    }

?> 
lolalola
  • 3,773
  • 20
  • 60
  • 96
  • 2
    possible duplicate of [Can I include code into a PHP class?](http://stackoverflow.com/questions/1957732/can-i-include-code-into-a-php-class) – Gordon Nov 05 '10 at 22:30
  • Making my answer CW because it's a duplicate. – Pekka Nov 05 '10 at 22:40

3 Answers3

4

What you are trying to do is not possible in PHP.

You would have to create multiple classes that extend each other:

class cars_base
 {....}

stop.php:

class cars_base_1 extends cars_base
 {....}

but this is rarely practical. Much rather try to build an object structure that is easy to split into separate modules that do not need to extend each other - or, if it clearly belongs into the same class, live with a lot of code in one file. With a good IDE, that's not that much of a problem.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
2

This is not possible in PHP, include doesn't work like a macro.

Florian
  • 3,145
  • 1
  • 27
  • 38
0

PHP 5.4 will support Traits, which offer what you want. But in the current stable version of PHP best you cen do is inheritance ;)

NikiC
  • 100,734
  • 37
  • 191
  • 225