A
A
Anton Shamanov2014-10-19 09:13:35
Zend Framework
Anton Shamanov, 2014-10-19 09:13:35

How to implement library class extension?

The Kohana framework has the following class extension mechanism:
The main file containing the class code is kohana/classes/Kohana_Test.php:

<?php
class Kohana_Test
{
   public function __construct()
   {
        echo 'kohana/classes/Kohana_Test.php';
   }
}

The class overridden in the application, app/classes/Test.php:
class Test extends Kohana_Test {
   public function __construct()
   {
        echo 'app/classes/Test.php';
   }
}

Dummy class, used when the class is not overridden, kohana/classes/Test.php:
class Test extends Kohana_Test
{
}

This approach allows you to change the Test class without unnecessary edits in the files that use it: Are there analogues for this mechanism that allow you to get rid of classes - "dummy"? How to achieve a similar effect when using, for example, the Zend framework?
$obj = new Test; // 'app/classes/Test.php'

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Shamanov, 2014-10-31
@SilenceOfWinter

<?php

function autoload($class)
{
    $prefix = 'Enso_';
    $file = $class . '.php';

    if (file_exists($file)) {
        include $file;
    } elseif (file_exists($prefix . $file)) {
        include $prefix . $file;
        class_alias($prefix . $class, $class);
    } else {
        return false;
    }
}

spl_autoload_register('autoload');

$obj = new Abc;
$obj = new Test;
$obj = new Enso_Test;

S
Shirshov Alexander, 2014-10-22
@Keanor

The question is not entirely clear, if you don’t need to redefine or add anything, then why do you need dummies?
ZF2 is tied to ServiceManager, dependencies are obtained from it by key. Key values ​​can be overridden in the application if you need it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question