tags:

views:

113

answers:

2

I'm a big fan of OOP in php, but i feel like defining class methods gets disorganized so fast. I have a pretty good background in OOP in C++, and i am pretty comfortable with how it is handled there, and am curious if there are ways to do it similarly in php.

To be more specific, here is what i mean. I like how in C++ you can define a class header (myclass.h) and then define the actual details of the functions in the implementation file (myclass.cc). Ive found that this can easily be replicated using interfaces in php, but i havent found a good solution for the following:

I like to organize my code in C++ in different files based on how they are accessed, so for example, public methods that can be called outside of the class would be in 1 place, and private methods would be organized somewhere else - this is personal preference.

Ive tried to define class methods in php like:

private function MyPHPClass::myFunction(){ }

when the definition isnt directly inside the class block( { } ), but i havent had any success doing this.

Ive been through all of the pages on php.net, but couldnt find anything like this. Im assuming that there is no support for something like this, but thought i would ask anyway.

thanks

+1  A: 

You can't do this. The class declarations are Java-like.

You have to put everything in one file or, at minimum, have some helper classes -- be they only static methods to which you forward or calls or with you deferring some implementation to encapsulated objects. You can also use the __call and __callstatic magic methods to reduce the size of your stubs, but that only works for public methods and I would recommend that you avoid magic methods.

EDI2: As RobertPitt pointed in a comment, you should consider alternative strategies that divide the functionality between several classes. It has the added advantage that it can make your code more decoupled. If you need, you can still unify the functionality of the several classes behind a façade.

EDIT: By using magic methods, I mean something like this:

class MyClassHelper {

    public function method_one(array $args) {
        ...
    }

    public function method_two(array $args) {
        ...
    }
}

class MyClass {
    /**
     * @var MyClassHelper
     */
    private $helper;

    private static $ALLOWED_METHODS = array("method_one" => NULL,
        "method_two" => NULL);

    public function __call($name, $arguments) {
        $name = strtolower($name);
        if (array_key_exists($name, self::$ALLOWED_METHODS) {
            $helper->$name($arguments);
        }
        else
            throw new Exception(...);
    }
}

I should recommend that you avoid this, there are many pitfalls to this (handling of references, no code completion).

Artefacto
Could you please extend on why to try and avoid magic_methods ? Thanks
RobertPitt
Magic methods are slower than regular ones, so it's generally a good idea to avoid them.
Maerlyn
I agree that they are slower than naming the actiual method but if the class is a generic class and may not need 100-200 methods individually but a __get/__set/__call system is much more effeciant when it comes down to that, For example $input->get->id('int'); rather than actually defining id() as a method just to fetch it fromt he storage would be in practical. I see your point but in some conexts its very good and worth it marginally.
RobertPitt
+1  A: 

Im not really a C++ / C# Programmer but interfaces in php i can give you an exampe to see if this helps.

Interface

interface IDatabase
{
   public function connect($dns = '');
   public function disconnect($flushCache = false); //Do not use braces, Code free in interfaces
}

Database abstract base class

abstract class Database
{
   //Some driver logic here to laod mysql,oracle etc
}

MySql Driver

class DBDriver_MySQl extends Database implements IDatabase
{
   public function connect($dns = '')
   {
     //Connection logic.
   }
   public function disconnect($flushDns)
   {
     //Disconnect Login
   }
}

Hope this is what your looking for.

RobertPitt
The problem is that you no multiple inheritance in PHP, as you do in C++.
Artefacto
http://stackoverflow.com/questions/90982/multiple-inheritance-in-php
RobertPitt
@RobertPitt you missed the point. Your specific example makes sense but does not address the general question of the OP. Basically, your are giving up your only chance on concrete inheritance. It would fail if the OP already wanted to extend some class and still wanted to segregate several implementations. Of course, you could say that he should consider other approaches, including the Strategy pattern because what he's trying to accomplish is bad practice, and I'd agree.
Artefacto
Oooh, Ok i understand now, Sorry about that, i haven't had my energy booster so i wasn't fully concentrating on the question... Sorry.
RobertPitt
Just curious, whats bad practice about it? I mean, its just code organization
kris