开发者

php OOP function declarations

开发者 https://www.devze.com 2023-01-02 04:18 出处:网络
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,

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 outsi开发者_开发百科de 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


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).


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消