-- debussy007 <[email protected]> wrote
(on Sunday, 12 July 2009, 08:40 AM -0700):
> I understand the namespace to avoid collisions between different libraries, 
> but I didn't understand your second point about extending classes. With or
> without namespaces I may extend a class to get a more specific behavior.

Right, but if you do it in your own namespace, you can then version that
class, use it in all your projects... and then keep it up-to-date with
API changes on the original.

> I have an error updating it to 1.8 because of the new namespaces added :-( 
> 
> Warning: include(My\Constants.php) [function.include]: failed to open
> stream: No such file or directory in
> C:\Data\websites\cms\library\Zend\Loader.php on line 83
> 
> $autoloader = Zend_Loader_Autoloader::getInstance(); 
> $autoloader->registerNamespace(array('My_', 'MyZend_'));
> 
> I have a class My_Constants and it's path is
> application/constants/Constants.php
> In the include path I added  './application/constants/'

Then the issue is that there isn't a 1:1 relationship between the class
name and the filename, and you have two options:

  * If you want it on the include_path, have it in My/Constants.php
    somewhere on that include_path.

  * If it's application-specific (which, based on your filesystem
    positioning, appears to be the case), create or modify your resource
    autoloader. If we go with the latter, try adding the following
    method to your application bootstrap:

    protected function _initAppAutoloader()
    {
        $al = new Zend_Application_Module_Autoloader(array(
            'namespace' => 'My',
            'basePath'  => dirname(__FILE__),
        ));
        $al->addResourceType('constant', 'constants', 'Constant');
    }

    Then, I'd rename your file to application/constants/App.php,
    with the class 'My_Constant_App', and you should be set.

> Matthew Weier O'Phinney-3 wrote:
> > 
> > -- debussy007 <[email protected]> wrote
> > (on Sunday, 12 July 2009, 02:09 AM -0700):
> >> I updated the library for one of my projects to 1.8 and I need some
> >> advice
> >> on renaming my classes with namespaces ...
> >> 
> >> Should I prefix all my classes (which are not part of a specific library)
> >> by
> >> the same namespace ? e.g. My_...
> >> 
> >> My_Constants
> >> My_Model_DbTable_Articles
> >> My_Plugin_LanguageHandler
> >> and so on ?
> > 
> > Typically, yes. :) It's a recommendation of the PHP project itself, as
> > well as PEAR and ZF. It helps with avoiding naming collisions, and also
> > allows you to extend classes locally to provide alternate behavior.
> > 
> > -- 
> > Matthew Weier O'Phinney
> > Project Lead            | [email protected]
> > Zend Framework          | http://framework.zend.com/
> > 
> > 
> 
> -- 
> View this message in context: 
> http://www.nabble.com/Namespace-help-tp24446973p24449740.html
> Sent from the Zend Framework mailing list archive at Nabble.com.
> 

-- 
Matthew Weier O'Phinney
Project Lead            | [email protected]
Zend Framework          | http://framework.zend.com/

Reply via email to