How to create a "truly extensible" class

I have read many articles on class development (I use php), with tags: "scalable, reliable, supported and extensible."

But as a beginner, I created classes that, in my opinion, are "simply abstracted." This means that I just split the heap or duplicate codes and put them in a class and provided methods for accessing common tasks.

The fact is, I can’t find a way to make my class extensible (I know the concept of abstract classes, etc., I even use them, but just to determine the methods that my other classes will follow). The thing is, I always find that I am editing the main class simply by adding functions. .

Any tips on extending my class? (I searched for this and everything that pops up, explains abstract classes, interfaces, and OOP, doesn't discuss pointers or some hints for creating extensible classes).

Oh, by the way, it’s easy for me, I started the “actual” oop programming 9 months ago (the university I was from started with OOP theory, but they worked as a PROCEDURE because it’s faster, and it corresponds to the overdue terms of the project, and this went on 4 years until I finished).

+3
source share
2 answers

You should check out the book Design Patterns: Elements of Reusable Object-Oriented Software

, , - .

, : , , , , , , ..

/ ?

, , , , . , , , :

, , API , / -. ( )?

  • , - ()
  • - ()
  • , ()

:

abstract class MyNs_Cache
{
    abstract public function Set($key, $data, $ttl);
    abstract public function Get($key);
    abstract public function Delete($key, $ttl);
}

. MyNs_Cache_Fs, MyNs_Cache_Apc MyNs_Cache_Memcache

class MyNs_Cache_Fs
{
     ...

    public function Set($key, $data, $ttl)
    {
        // here I use fopen/fwrite/etc. to create the cached data
    }

    public function Get($key)
    {
        // here I retrieve the file from the filesystem (if it exists)
    }

    public function Delete($key) { ... }
 }

. FileSystem. .

class MyNs_Cache_Apc
{
    ...

    public function Set($key, $data, $ttl)
    {
        return apc_add($key, $data, $ttl); // NOT A FILESYSTEM CALL
    }

    public function Get($key) { ... } // you get the idea.

    // This is specific to APC, so I add the functionality HERE
    // NOT in my main Caching class.
    public function PurgeCache()
    {
        return apc_clear_cache();
    }
}

APC , , (set/get/delete), ( FileSystem memcached)

class MyNs_Cache_Memcache
{
    // Memcached needs a pool of servers. APC and filesystem don't.
    private $servers = array(..);

    // It also uses a memcached object.
    private $conn;

    public function __construct()
    {
        $this->conn = new Memcached;

        foreach ($this->$servers as $server)
            $this->AddServer($server);
    }
    ...  // we do all the standard stuff using memcached methods

    // We also want to be able to manage our connection pool
    public function AddServer($server)
    {
        $this->conn->addServer(...);
    }

    // And in some cases, we use inc/dec from memcached
    // APC doesn't have this, and it makes little sense in a filesystem
    public function Increment($key) { ... }
}

, $obj- > Get ('some_key'), .

, , , .

+2

, , , :

class A {
 public function filter_string($str){
  return str_replace ('foo', 'bar', $str);
 }
}

class B extends A {
 public function filter_string($str){
  return str_replace ('machin', 'chose', parent::filter_string ($str));
 }
}

$a = new A;
echo $a->filter_string('foo machin'); // echoes 'bar machin'

$b = new B;
echo $b->filter_string('foo machin'); // echoes 'bar chose'
+1

Source: https://habr.com/ru/post/1734814/


All Articles