No. You cannot include files in the body of a class.
In the file defining the class, you can include only those body files or outside the body of the class .
From your description, I want you to want this:
<?php // MyClass.php class MyClass { protected $_prop; include 'myclass-methods.php'; } <?php // myclass-methods.php public function myMethod() { $this->$_prop = 1; }
Running this code will result in
Parse error: syntax error, unexpected T_INCLUDE, expecting T_FUNCTION
What is possible but it
<?php // MyClass.php class MyClass { protected $_prop; public function __construct() // or any other method { include 'some-functions.php'; foo($b); // echoes 'a'; } } <?php // some-functions.php $b = 'a'; function foo($str) { echo $str; }
By doing this, it imports the contents of the include file into the method scope, and not into the class scope. You can include functions and variables in the included file, but not methods. You could, but should not, paste all the scripts into it and change the way, for example,
<?php // MyClass.php // ... public function __construct($someCondition) { // No No Code here include ($someCondition === 'whatever') ? 'whatever.php' : 'default.php'; } // ... <?php // whatever.php echo 'whatever'; <?php // default.php echo 'foo';
However, fixing the class in such a way as to exhibit different behavior is not how you should do it in OOP. This is simply wrong and you have to clean your eyes.
Since you want to dynamically change behavior, extending a class is also not a good option (see below for why). What you really want to do is write an interface and make your class using objects that implement this interface, so as to make sure that the appropriate methods are available. This is called the Strategy Pattern and works as follows:
<?php
Now you have a contract according to which all imitation actions must obey, namely, have a meow method. Then define the meow behavior:
<?php // RegularMeow.php class RegularMeow implements Meowing { public function meow() { return 'meow'; } }
Now, to use it, use:
<?php // Cat.php class Cat { protected $_meowing; public function setMeowing(Meowing $meowing) { $this->_meowing = $meowing; } public function meow() { $this->_meowing->meow() } }
By adding a TypeHint to SetMeowing Meowing , you are convinced that the passed parameter implements the Meowing interface. Let it determine another behavior by the method:
<?php // LolkatMeow.php class LolkatMeow implements Meowing { public function meow() { return 'lolz xD'; } }
Now you can easily change the behavior as follows:
<?php require_once 'Meowing.php'; require_once 'RegularMeow.php'; require_once 'LolkatMeow.php'; require_once 'Cat.php'; $cat = new Cat; $cat->setMeowing(new RegularMeow); echo $cat->meow;
Although you could also solve the above inheritance by specifying the BaseCat and meow abstract method and then deriving the specific RegularCat and Lolkat classes from it, you need to think about what you want to achieve. If your cats will never change the way they meow, continue to use inheritance, but if your RegularCat and Lolkat should be able to make arbitrary balls, use the strategy template.
For more design patterns in PHP, check out these resources: