Using PHP namespaces in a Zend Framework (v1) application
Asked Answered
P

3

13

Is it possible in the current stable version of the Zend Framework (1.11), to work with application classes using PHP namespaces?

Application\Form\Abc instead of Application_Form_Abc
Application\Model\Xyz instead of Application_Model_Xyz
etc.

Starting from v1.10, ZF supports autoloading namespaces, and it's working fine when including namespaced libraries, but I was unsuccessful when trying to do the same job with application classes.

Pod answered 28/6, 2011 at 21:53 Comment(5)
duplicate of #1692293Untinged
@Rufinus: nope, I did have a look at this question, which is 2 years old. The namespace autoloading support was not out at this time, and the author was asking if it was possible to modify ZF to support this feature. I'm asking if a subset of this feature is built-in now; they're talking about the internal ZF classes, while I just want my application classes to use namespaces, I don't care extending ZF's non-namespaced classes.Pod
you just have to take care of the autoloader, look for doctrine 2.0 integration tutorials, they have use namespaces and have an own autoloader which is used by zend_loader.Untinged
@Rufinus: as I mentioned, the autoloader currently shipped with ZF already handles namespaces. But I'm talking about the ZF-specific mapping from e.g. Application_Form_Abc to /application/forms/Abc.php, which is different from the standard mapping.Pod
This more recent SO question (#6409924) discusses it, but (so far) comes to the conclusion that @Benjamin notes: the ZF1 autoloader does handle namespaces, but Zend_Loader_Autoloader_Resource does not.Timberhead
P
8

Actually there is a simple workaround suggested by Dmitry on the ZF issue tracker:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initAutoloader()
    {
        $loader = function($className) {
            $className = str_replace('\\', '_', $className);
            Zend_Loader_Autoloader::autoload($className);
        };

        $autoloader = Zend_Loader_Autoloader::getInstance();
        $autoloader->pushAutoloader($loader, 'Application\\');
    }
}

Works like a charm for me!

Pod answered 29/6, 2011 at 12:20 Comment(0)
S
6

The standard autloader introduced in 1.12 allows you to use namespaces with minimal effort:

require 'Zend/Loader/AutoloaderFactory.php';
Zend_Loader_AutoloaderFactory::factory([
    'Zend_Loader_StandardAutoloader' => [
        'autoregister_zf' => true,
        'namespaces' => [
            'Application' => '/path/to/Application/src',
        ]
    ]
]);

Now you can use Application\Form\Abc instead of Application_Form_Abc & Application\Model\Xyz instead of Application_Model_Xyz etc.

Directory/File structure examples:

path/to/Application/src/Form/Abc.php

<?php
namespace Application/Form;
class Abc {}

path/to/Application/src/Model/Xyz.php

<?php
namespace Application/Model;
class Xyz {}
Stigmatize answered 4/6, 2013 at 21:30 Comment(0)
T
0

For those wanting your controllers to work and not just the forms and models :) Take a look at the Zend\Controller\Dispatcher\Standard.php:174

public function formatClassName($moduleName, $className)

You would need to swap the _ with "\" as well in order to get the correct class name.

Trapezoid answered 29/12, 2022 at 17:28 Comment(2)
I would advise to move away from ZF1 in 2022, if you can ;-)Pod
Some projects you cannot move they've been working forever so to maintain the old code and implement something new is nice.Trapezoid

© 2022 - 2024 — McMap. All rights reserved.