skip to Main Content

We would like to create a module for our project called Memcached. This way we can have namespaced services (e.g. MemcachedServiceGet) which perform actions using php’s installed Memcached class.

However, we notice the following lines in zendframework/zend-modulemanager/src/Listener/ModuleResolverListener.php

if (class_exists($moduleName)) {
    return new $moduleName;
}

This means that if we name our module Memcached, then loading the module simply will not work. The module will be instantiated as a Memcached object rather than the desired MemcachedModule object.

So, is there any way we can simply name our module Memcached? Or, do we need to be more creative and name our module something like MemcachedModule? We would prefer not to do the latter since none of our other modules have this Module suffix.

2

Answers


  1. Chosen as BEST ANSWER

    Zend Framework 3 has been patched so that a module may be named the same as an existing class. The following code was added to the ModuleResolverListener:

    $class = sprintf('%sModule', $moduleName);
    if (class_exists($class)) {
        return new $class;
    }
    

    So now, for example, we can create our own Zend Module called MemcachedModule which will not conflict with php's built in Memcached class.

    For more info, view the patch on GitHub.


  2. Module is Zend thing written using PHP Classes. You cant have two classes with same name, so you also cant import “module class” and “own class” with same name, but this is related only if you working in one namespace. You can access classes by full namespace. Here is some example.

    <?php
    namespace myspace;
    
    class TheOne {
       function __construct() {
           print "my one createdn";
       }
    }
    
    namespace otherspace;
    use myspaceTheOne as MyOne;
    
    class TheOne {
       function __construct() {
           print "other one createdn";
       }
    }
    
    new TheOne(); //other one created
    new MyOne();  //my one created
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search