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

[{'answer_id': 71502, 'author': 'Silver Dragon', 'author_id': 9440, 'author_profile': 'https://Stackoverflow.com/users/9440', 'pm_score': 1, 'selected': False, 'text': '<p>No. </p>\n\n<p>You can extend previously declared classes, though, if that helps.</p>\n'}, {'answer_id': 71545, 'author': 'Michał Niedźwiedzki', 'author_id': 2169, 'author_profile': 'https://Stackoverflow.com/users/2169', 'pm_score': 3, 'selected': False, 'text': '<p>No, as of PHP 5.2. However, you may use <code>__call</code> magic method to forward call to arbitrary function or method.</p>\n\n<pre><code>class A {\n\n    public function __call($method, $args) {\n        if ($method == \'foo\') {\n            return call_user_func_array(\'bar\', $args);\n        }\n    }\n\n}\n\nfunction bar($x) {\n    echo $x;\n}\n\n$a = new A();\n$a-&gt;foo(\'12345\'); // will result in calling bar(\'12345\')\n</code></pre>\n\n<p>In PHP 5.4 there is support for <em>traits</em>. 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 <a href="http://www.stefan-marr.de/artikel/rfc-traits-for-php.html" rel="nofollow noreferrer">here</a>.</p>\n'}, {'answer_id': 71550, 'author': 'deresh', 'author_id': 11851, 'author_profile': 'https://Stackoverflow.com/users/11851', 'pm_score': 0, 'selected': False, 'text': '<p>No it is not posible. if you define function/method outside class construct it becomes global function.</p>\n'}, {'answer_id': 71551, 'author': 'Paul Dixon', 'author_id': 6521, 'author_profile': 'https://Stackoverflow.com/users/6521', 'pm_score': 2, 'selected': False, 'text': '<p>You could perhaps override <a href="http://php.net/manual/en/language.oop5.overloading.php" rel="nofollow noreferrer">__call or __callStatic</a> 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.</p>\n\n<p>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...</p>\n\n<pre><code>class Foo {\n\n    public function __call($method, $args) {\n        $delegate="FooDelegate_".$method;\n        if (class_exists($delegate))\n        {\n             $handler=new $delegate($this);\n             return call_user_func_array(array(&amp;$handler, $method), $args);\n        }\n\n\n    }\n\n}\n</code></pre>\n'}, {'answer_id': 71559, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 0, 'selected': False, 'text': "<p>C++ can't do this either. Did you mix up declaration with de<em>finition</em>?</p>\n"}, {'answer_id': 71611, 'author': 'joelhardi', 'author_id': 11438, 'author_profile': 'https://Stackoverflow.com/users/11438', 'pm_score': 0, 'selected': False, 'text': '<p>No, as everyone has said, it is not strictly possible.</p>\n\n<p>However, you can do <a href="http://www.symfony-project.org/book/1_0/17-Extending-Symfony" rel="nofollow noreferrer">something like this</a> 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.</p>\n'}, {'answer_id': 1267223, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>Yes it is possible to add a method to a PHP class after it is defined. You want to use <a href="http://www.php.net/manual/en/ref.classkit.php" rel="noreferrer">classkit</a>, 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).</p>\n\n<pre><code>&lt;?php\nclass A { }\nclasskit_method_add(\'A\', \'bar\', \'$message\', \'echo $message;\', \n    CLASSKIT_ACC_PUBLIC); \n$a = new A();\n$a-&gt;bar(\'Hello world!\');\n</code></pre>\n\n<p>Example from the PHP manual:</p>\n\n<pre><code>&lt;?php\nclass Example {\n    function foo() {\n        echo "foo!\\n";\n    }\n}\n\n// create an Example object\n$e = new Example();\n\n// Add a new public method\nclasskit_method_add(\n    \'Example\',\n    \'add\',\n    \'$num1, $num2\',\n    \'return $num1 + $num2;\',\n    CLASSKIT_ACC_PUBLIC\n);\n\n// add 12 + 4\necho $e-&gt;add(12, 4);\n</code></pre>\n'}, {'answer_id': 8020459, 'author': 'jocap', 'author_id': 305047, 'author_profile': 'https://Stackoverflow.com/users/305047', 'pm_score': 2, 'selected': False, 'text': '<p>As PHP 5.3 supports closures, you can dynamically define instance methods as variables holding closures:</p>\n\n<pre><code>$class-&gt;foo = function (&amp;$self, $n) {\n  print "Current \\$var: " . $self-&gt;var . "\\n";\n  $self-&gt;var += $n;\n  print "New \\$var:     " .$self-&gt;var . "\\n";\n};\n</code></pre>\n\n<p>Taking <code>$self</code> (you can\'t use <code>$this</code> outside object context) as a reference (<code>&amp;</code>), you can modify the instance.</p>\n\n<p>However, problems occur when you try to call the function normally:</p>\n\n<pre><code>$class-&gt;foo(2);\n</code></pre>\n\n<p>You get a fatal error. PHP thinks <code>foo</code> is a method of <code>$class</code>, because of the syntax. Also, you must pass the instance as the first argument.</p>\n\n<p>There is luckily a special function for calling functions by name called <code>call_user_func</code>:</p>\n\n<pre><code>call_user_func($class-&gt;foo, &amp;$class, 2);\n# =&gt; Current $var: 0\n# =&gt; New $var:     2\n</code></pre>\n\n<p>Just remember to put <code>&amp;</code> before the instance variable.</p>\n\n<p>What\'s even easier is if you use the <code>__call</code> magic method:</p>\n\n<pre><code>class MyClass {\n  public function __call ($method, $arguments)  {\n    if (isset($this-&gt;$method)) {\n      call_user_func_array($this-&gt;$method, array_merge(array(&amp;$this), $arguments));\n    }\n  }\n}\n</code></pre>\n\n<p>Now you can call <code>$class-&gt;foo(2)</code> instead. The magic <code>__call</code> method catches the call to an unknown method, and calls the closure in the <code>$class-&gt;foo</code> variable with the same name as the called method.</p>\n\n<p>Of course, if <code>$class-&gt;var</code> was private, the closure in stored in the <code>$class-&gt;foo</code> variable wouldn\'t be able to access it.</p>\n'}]
['https://Stackoverflow.com/questions/71478', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']