Methods defined by an outer class?

I am wondering if php methods are ever defined outside the class body, as they are often executed in C ++. I understand that this question is the same as defining class methods in PHP . But I believe that his original question was “declared” instead of “define,” so all the answers seem a little inappropriate.

Update:

My idea of ​​defining and declaring was probably erroneous. But defining outside the body of the class, I had in mind something equivalent to C ++

class CRectangle { int x, y; public: void set_values (int,int); int area () {return (x*y);} }; void CRectangle::set_values (int a, int b) { x = a; y = b; } 

All php code examples have code inside the class body, similar to the built-in C ++ function. Even if there wasn’t a functional difference between the two in PHP, it’s just a matter of style.

+6
c ++ oop php class
source share
7 answers

The presence of method declarations in header files separately from their implementation, as far as I know, is quite unique to C / C ++. All other languages ​​that I know do not have it at all or only in a limited form (for example, Java and C # interfaces)

+4
source share

Here is a terrible, ugly hack that should never be used. But you asked for it!

 class Bar { function __call($name, $args) { call_user_func_array(sprintf('%s_%s', get_class($this), $name), array_merge(array($this), $args)); } } function Bar_foo($this) { echo sprintf("Simulating %s::foo\n", get_class($this)); } $bar = new Bar(); $bar->foo(); 

What I've done? In any case, to add new methods, simply attach them to the class name and underscore. The first argument to the function is a reference to $this .

+6
source share

I came across this question, looking for a way to separate the declaration and implementation of class methods. What for? For ease of reading code. When the declarations are in a separate file or at the top of the class file, someone who wants to use the class should not wade through the entire implementation to find out what methods are offered.

I found something useful though: PHP interface classes. I do not think that they are intended for this, but they serve the purpose: the interface class defines all the methods, and then implements their “real” class. Here is an article about it:

http://www.davegardner.me.uk/blog/2010/11/21/why-you-should-always-use-php-interfaces/

+2
source share

It is possible, but very, very hacky and not recommended. There is no reason for this.

+1
source share

First of all, in PHP, all class methods must be defined and implemented in the class structure ( class x { } ). This is in no way similar to C ++, where you have implementations ( *.cpp ) separate from the definitions ( *.h ).

+1
source share

Native PHP does not support such functions, but with PHP5.4 you can dynamically add methods to an object. As an example, you can see the following: https://github.com/ptrofimov/jslikeobject

+1
source share

Not. You can consider 'declare' and 'define' interchangeably in this matter.

0
source share

All Articles