Defining class methods in PHP

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

+7
php class
Sep 16 '08 at 11:46
source share
8 answers

No, starting with PHP 5.2. However, you can use the __call magic method to redirect a call to an 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') 

PHP 5.4 supports tag support. Trait is an implementation of a method (s) that cannot be created as a separate object. Instead, a trait can be used to extend a class with a stored implementation. More on features here .

+8
Sep 16 '08 at 11:59
source share

Yes, after defining it, you can add the method to the PHP class. You want to use classkit , which is an "experimental" extension. Apparently, this extension is not enabled by default, however, it depends on whether you can compile a custom PHP file or load PHP DLLs if it is on windows (for example, Dreamhost allows you to create your own PHP binaries, and they are quite easy to configure )

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

An example from the PHP manual:

 <?php class Example { function foo() { echo "foo!\n"; } } // create an Example object $e = new Example(); // Add a new public method classkit_method_add( 'Example', 'add', '$num1, $num2', 'return $num1 + $num2;', CLASSKIT_ACC_PUBLIC ); // add 12 + 4 echo $e->add(12, 4); 
+5
Aug 12 '09 at 16:23
source share

You may be able to override __ call or __callStatic to find the missing method at runtime, but you will need to create your own system to find and call the code. For example, you can load the Delegate class to handle a method call.

Here's an example - if you tried calling $ foo-> bar (), the class tried to create the class FooDelegate_bar and bar () on it with the same arguments. If you have automatic class loading installed, the delegate can live in a separate file until you need it ...

 class Foo { public function __call($method, $args) { $delegate="FooDelegate_".$method; if (class_exists($delegate)) { $handler=new $delegate($this); return call_user_func_array(array(&$handler, $method), $args); } } } 
+3
Sep 16 '08 at 12:01
source share

No.

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

+1
Sep 16 '08 at 11:51
source share

Since PHP 5.3 supports locking, you can dynamically define instance methods as variables that hold closures:

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

By taking $self (you cannot use $this context of an external object) as a reference ( & ), you can change the instance.

However, trying to call a function usually causes problems:

 $class->foo(2); 

You get a fatal error. PHP considers foo to be a $class method, because of the syntax. In addition, you must pass the instance as the first argument.

Fortunately, there is a special function for calling functions called call_user_func :

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

Remember to put & in front of the instance variable.

Even simpler 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) . The __call magic method catches an invocation of an unknown method and causes a closure in the variable $class->foo with the same name as the called method.

Of course, if $class->var was closed, closing in the $class->foo variable stored in the variable could not access it.

+1
Nov 05 2018-11-11T00:
source share

No, It is Immpossible. if you define a construct outside the class of the function / method class, it becomes a global function.

0
Sep 16 '08 at 12:00
source share

C ++ cannot do this either. Did you mix the ad with the definition?

0
Sep 16 '08 at 12:02
source share

No, as everyone said, this is not entirely possible.

However, you can do something like this to emulate mixin in PHP or add methods to the class at runtime, which is about as close as you are going to. Basically, it just uses design patterns to achieve the same functionality. Zope 3 does something similar to emulating mixins in Python, in another language that does not directly support them.

0
Sep 16 '08 at 12:13
source share



All Articles