PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun Sep 24, 2017 8:02 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Thu Mar 24, 2016 3:39 am 
Offline
Forum Newbie

Joined: Mon Jun 16, 2014 10:13 am
Posts: 2
Hello,

I would like to ask several questions regarding programming in OOP PHP. Last time, I have written several classes (i.e. for cache and logging support), but I have not used namespaces. Now I would like to introduce them and I wonder if it makes any sense of creating as many namespaces as are available in other projects available on the net for free. Actually I am thinking about creating \Framework namespace, where all my classes could fit w/o worrying about name conflicts. However, someone might suggest to use some sub-namespaces, like \Framework\Cache or even \Framework\Cache\Driver\Apc. This leads to many namespaces, containing only few (or even just one) class inside. Do you see any reason for creating as many namespaces? What would you recommend?

Final question is more about design. As I have written, I have several classes that provides support for caching. One, main Cache class and some helpers. I have also written drivers for file-based cache, apc, etc. Actually there is Cache::loadDriver method, that takes care about loading proper driver. It is sufficient, to pass 'apc' string as parameter, and it will automatically load the proper driver, by simply creating an object:
Syntax: [ Download ] [ Hide ]
$driver = 'apc';
$classname = '\Framework\Cache\' . strtoupper($driver);
$this->driver = new $classname();

My idea was to do it automatically, and not bother anyone about what to load. I think that even, when I choose to pass already instantiated object, code like above would be necessary but in other part of application. If I would like to let the user choose cache driver for application, and he would be able to click it somewhere via web panel, then still the application would need to match driver name with proper class name and namespace. But I might be wrong? What is your opinion?

I am looking forward to hear your opinions.


Top
 Profile  
 
PostPosted: Thu Mar 24, 2016 6:27 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6348
Location: Montreal, Canada
Top level namespaces help avoid collisions with similar packages from other projects for sure, but I also find namespaces help add some semantic meaning, so I nest as many deep as I need to. Even then, it's rare I'll find myself more than four or five deep, which is totally manageable. I think your suggestion of Framework\Cache\Driver\Apc is perfectly reasonable.

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Thu Mar 24, 2016 8:32 am 
Offline
Forum Newbie

Joined: Mon Feb 15, 2010 7:01 am
Posts: 16
You don't have any reason to create namespaces in a deeper level except if that level is mandatory and can be alternated (even in this case it could be a namespace by its own). Don't forget that inside the same namespace you don't have to (and actually you shouldn't) use the “use” clause for classes from within the same namespace. Outside of that namespace I always use aliasing to that because it allows you to refer in a namespace in an abstract way.

Not everything needs to be in a namespace. Everything that is in the domain of the src folder of an application (it isn't module or plug in) , or in the framework side doesn't have any sense to be in a namespace. Does you framework lies inside the src of each project? If so then yes it has a meaning to has its own namespace. If your framework is outside src and src is using it , then it doesn't.


Top
 Profile  
 
PostPosted: Thu Mar 24, 2016 4:36 pm 
Offline
Forum Newbie

Joined: Mon Jun 16, 2014 10:13 am
Posts: 2
Thank you very much for your tips regarding namespaces.
After reading your post, Celauran, I imagine namespace&class hierarchy as follows:

Syntax: [ Download ] [ Hide ]
namespace Framework\Cache;
abstract class Common {}

namespace Framework\Cache;
interface Driver {}

namespace Framework;
final class Cache extends Cache\Common {}

namespace Framework\Cache\Driver;
use Framework\Cache;
class Apc extends Cache\Common implements Cache\Driver


What is more, namespaces reflect the directory structure, so that Framework\Cache\Driver\Apc would be saved under cache/driver/apc.php. This allows to write very simple automatic class loader (skip leading Framework\, replace \ with DIRECTORY_SEPARATOR, strtolower, add .php at the end).


BTW: What would you also say about the second question?


Top
 Profile  
 
PostPosted: Sun Mar 27, 2016 3:18 pm 
Offline
Forum Newbie

Joined: Mon Feb 15, 2010 7:01 am
Posts: 16
Its common in programming to use one thing were it hasn't nothing to do with only because you think it makes your life easier in another field. Use each aspect in each own domain. PHP didn't introduced namespaces in order for that kind of use in autoloader. In fact using namespaces only for that purpose is something against for what “namespaces” mean in programming.

There was the common OOP knowledge (and yes Zend made it as flag) in autoloading. For example a class named Model_Shop_Object_Product would be in src/Model/Shop/Object/Product.php . Frameworks normally are outside the src directory (not in PHP) witch means that giving a hint to the autoloader (with underscore or with the name of the framework) would understand where to load it.

The thing with namespaces comes out of modules or / and plugins. If you have a rediculus long path , e.g. Plugin/Apluginlikefacebook/Something/Something/Something.php would be a waste of time to write Plugin_ Apluginlikefacebook_ Something_ Somethingelse_ Something as a class name. So you can define that use Plugin_ Apluginlikefacebook_ Something as something , any time you like to refer to a class under it you can say, something/Somethingelse_ Something . Inside the same namespace you don't have to (and actually you shouldn't) use the use clause , and of course you don't have to use the root \ before each class name if you are in root.

PHP is a great language that can be used from amateurs but also from programmers as well. I don't see any reason programmers using namespaces outside their OOP context other than they are amateurs.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group