-- Dave kennedy <d...@ilovelegion.com> wrote
(on Friday, 01 May 2009, 03:40 AM -0700):
> Gabriel Malkas wrote:
> > Dave kennedy a écrit :
> > > Now I am tearing my hair out over this. I started refactoring a small
> > > project
> > > to 1.8. Before I had included my models directory in the include path so
> > > they could be called whenever wherever.
> > >
> > > My structure is
> > >
> > > application
> > >  - controllers
> > >  - models
> > >   - Sample.php
> > >  - forms
> > >  - configs
> > >  - layouts
> > >  - views
> > >  - Bootstrap.php
> > > public
> > >  - .htaccess
> > >  - index.php
> > >
> > > Im not using a modular structure and do not wish to. I read up on Zend
> > > Application and implemented it with the bootstrap I got a test project
> > > going, my problem is I can no longer autoload my model classes
> > >  
> > > before I would use $sample = new Sample();
> > >
> > > Now I can no longer get that working. I know it is to do with the
> > > namespace autoloading but just cannot get my head round it.... any
> > > help would be greatly appreciated.
> > 
> > I don't think it is the best solution to this problem, but it works : 
> > add a namespace to your models :) It makes sense. For example, if you 
> > have a model User or whatever, and your application is called App, then 
> > call your model App_User. Then, just add the namespace App to your 
> > configuration file.
> > 
> > I am not sure it is a good practice though. Maybe it is against Zend 
> > Framework philosophy, I don't know. It is up to you ;)
> 
> Thank you for the reply Gabriel. I had actually done this and I guess I must
> have too much rails in me I really do not like $user = new App_User(); As I
> feel it would not make my code as portable as I would like. Ideally I would
> like to keep $user = new User();
> 
> I think my answer lies in here somewhere 
> http://framework.zend.com/manual/en/zend.loader.autoloader-resource.html#zend.loader.autoloader-resource.usage
> http://framework.zend.com/manual/en/zend.loader.autoloader-resource.html#zend.loader.autoloader-resource.usage
>  
> 
> but not sure... ill maybe look at it after my head has cleared slightly :)

Using a project or vendor namespace is something we highly recommend,
and there are some pretty strong reasons for doing so.

  * Helps prevent naming collisions. One reason PHP is popular is
    because of the number of unzip and go applications written for it. A
    common pattern is to mix and match several applications to create a
    site. However, if you need to integrate these apps, without proper
    namespacing, you can create naming collisions.
  * Easier extension. If you want to re-use a model later in another
    application, but need to modify some behavior, the best path is to
    extend the original. It's easier to do so when the original and the
    extending class simply vary the namespace: Blog_User vs Admin_User.
  * Heck, even php.net recommends the practice:
    http://us3.php.net/manual/en/userlandnaming.tips.php

The resource autoloader is designed to do several things. First, it
expects a namespace prefix. Dirty little secret: you can provide an
empty string for this. Second, it expects that there are related
components (resources) under its base path, and each has a
component-level namespace prefix. The combination of a common prefix and
a component prefix helps ensure that the class names are unique, and
that their purpose within the project is clearly known from their name.

Yes, $user = new User() is nice and succinct, but it's non-portable and
tells you nothing about where it falls inside the project structure.

-- 
Matthew Weier O'Phinney
Project Lead            | matt...@zend.com
Zend Framework          | http://framework.zend.com/

Reply via email to