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';
    }

?> 
2
  • 2
    possible duplicate of Can I include code into a PHP class? Commented Nov 5, 2010 at 22:30
  • Making my answer CW because it's a duplicate. Commented Nov 5, 2010 at 22:40

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

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 ;)

1 Comment

Wow. Never heard of this, and now I WANT.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.