Home » Php » Defining class methods in PHP

Defining class methods in PHP

Posted by: admin July 12, 2020 Leave a comment


Is it possible in PHP (as it is in C++) to declare a class method OUTSIDE the class definition?

How to&Answers:

No, as of PHP 5.2. However, you may use __call magic method to forward call to arbitrary function or method.

class A {

    public function __call($method, $args) {
        if ($method == 'foo') {
            return call_user_func_array('bar', $args);


function bar($x) {
    echo $x;

$a = new A();
$a->foo('12345'); // will result in calling bar('12345')

In PHP 5.4 there is support for traits. Trait is an implementation of method(s) that cannot be instantiated as standalone object. Instead, trait can be used to extend class with contained implementation. Learn more on Traits here.


Yes it is possible to add a method to a PHP class after it is defined. You want to use classkit, which is an “experimental” extension. It appears that this extension isn’t enabled by default however, so it depends on if you can compile a custom PHP binary or load PHP DLLs if on windows (for instance Dreamhost does allow custom PHP binaries, and they’re pretty easy to setup).

class A { }
classkit_method_add('A', 'bar', '$message', 'echo $message;', 
$a = new A();
$a->bar('Hello world!');

Example from the PHP manual:

class Example {
    function foo() {
        echo "foo!\n";

// create an Example object
$e = new Example();

// Add a new public method
    '$num1, $num2',
    'return $num1 + $num2;',

// add 12 + 4
echo $e->add(12, 4);


You could perhaps override __call or __callStatic to locate a missing method at runtime, but you’d have to make up your own system for locating and calling the code. For example, you could load a “Delegate” class to handle the method call.

Here’s an example – if you tried to call $foo->bar(), the class would attempt to create a FooDelegate_bar class, and call bar() on it with the same arguments. If you’ve got class auto-loading set up, the delegate can live in a separate file until required…

class Foo {

    public function __call($method, $args) {
        if (class_exists($delegate))
             $handler=new $delegate($this);
             return call_user_func_array(array(&$handler, $method), $args);




As PHP 5.3 supports closures, you can dynamically define instance methods as variables holding closures:

$class->foo = function (&$self, $n) {
  print "Current $var: " . $self->var . "\n";
  $self->var += $n;
  print "New $var:     " .$self->var . "\n";

Taking $self (you can’t use $this outside object context) as a reference (&), you can modify the instance.

However, problems occur when you try to call the function normally:


You get a fatal error. PHP thinks foo is a method of $class, because of the syntax. Also, you must pass the instance as the first argument.

There is luckily a special function for calling functions by name called call_user_func:

call_user_func($class->foo, &$class, 2);
# => Current $var: 0
# => New $var:     2

Just remember to put & before the instance variable.

What’s even easier is if you use the __call magic method:

class MyClass {
  public function __call ($method, $arguments)  {
    if (isset($this->$method)) {
      call_user_func_array($this->$method, array_merge(array(&$this), $arguments));

Now you can call $class->foo(2) instead. The magic __call method catches the call to an unknown method, and calls the closure in the $class->foo variable with the same name as the called method.

Of course, if $class->var was private, the closure in stored in the $class->foo variable wouldn’t be able to access it.



You can extend previously declared classes, though, if that helps.


No it is not posible. if you define function/method outside class construct it becomes global function.


C++ can’t do this either. Did you mix up declaration with definition?


No, as everyone has said, it is not strictly possible.

However, you can do something like this to emulate a mixin in PHP or add methods to a class at runtime, which is about as close as you’re going to get. Basically, it’s just using design patterns to achieve the same functionality. Zope 3 does something similar to emulate mixins in Python, another language that doesn’t support them directly.