8

I am wondering if php methods are ever defined outside of the class body as they are often done in C++. I realise this question is the same as Defining class methods in PHP . But I believe his original question had 'declare' instead of 'define' so all the answers seem a bit inappropriate.

Update:

Probably my idea of define and declare were flawed. But by define outside of the class body, i meant something equivalent to the 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 the examples of php code have the the code inside the class body like a C++ inlined function. Even if there would be no functional difference between the two in PHP, its just a question of style.

Community
  • 1
  • 1
zenna
  • 9,006
  • 12
  • 73
  • 101
  • that's pretty much the same question – Paul Dixon Aug 14 '09 at 20:59
  • Please define the difference between "declare" and "define" ;) – matpie Aug 14 '09 at 21:02
  • I don't know the answer to this question, but I agree that the incorrect usage of *declare* in the other question makes it hard for me to know how to interpret the answers. The responses here so far don't clear it up. – Darryl Aug 14 '09 at 21:04
  • See: http://stackoverflow.com/questions/71478/defining-class-methods-in-php and http://stackoverflow.com/questions/2938004/how-to-add-a-new-method-to-a-php-object-on-the-fly – kenorb Oct 11 '13 at 10:59

7 Answers7

5

I stumbled upon this question while looking for a way to separate declaration and implementation of class methods. Why? For the sake of code readability. When the declarations are in a separate file, or at the top of the class file, someone looking to use the class does not have to wade through the whole implementation to find out which methods are offered.

I did find something useful though: PHP interface classes. I don't think they are designed for it, but they serve the purpose: an interface class defines all the methods, and then the "real" class implements them. Here's an article about it:

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

littlegreen
  • 7,290
  • 9
  • 45
  • 51
5

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 have I done? Anyway, to add new methods, just prefix them with the name of the class and an underscore. The first argument to the function is a reference to $this.

Justin Poliey
  • 16,289
  • 7
  • 37
  • 48
4

Having the declaration of methods in header files separate from their implementation is, to my knowledge, pretty unique to C/C++. All other languages I know don't have it at all, or only in limited form (such as interfaces in Java and C#)

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
1

It's possible, but very, very hacky and not recommended. No reason to do it either.

Jani Hartikainen
  • 42,745
  • 10
  • 68
  • 86
1

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

matpie
  • 17,033
  • 9
  • 61
  • 82
1

Natively PHP doesn't support features like this, but since PHP5.4 you can dynamically add methods to object. As an example, you can look at this: https://github.com/ptrofimov/jslikeobject

0

No. You can consider 'declare' and 'define' to be interchangeable in that question.

chaos
  • 122,029
  • 33
  • 303
  • 309