Jump to content

Namespacing and PHP.


AncientTom

Recommended Posts

I have been away from php for a bit and started working with Java. Now I'm back to php and working on trying to comprehend the structure of the newly remodeled concrete5 CMS. In their original old creation, their work hinted at the use of php namespacing but wasn't really using the power of namespacing with php. I have now returned a couple of years later to 'the newly created from the ground up' concrete5 and find that their new framework is created totally with the use of namespaces. So, now I am back to Lynda.com to bring myself up to speed with the way namespacing is handled in php.

 

When I started learning Java it became obvious to me that at compile time, the namespace that was declared in the package became the literal path to the classes in the project. Once I saw this happening, namespacing became very clear to me how things work in Java .jar files, Since php scripts get interpreted at run time and not compiled, It looks like namespace declarations are more or less path aliases and the path tree and the namespace can be two different things. This can make things very hard to follow when an author can declare a namespace that is different than the actual path of a class when trying to follow someone else's work. For instance, in concrete5.7 I am trying to back-trace code usage and I find declarations of namespaces that do not match the file structure. While I am very impressed with the new c5, I am finding, as a result of this, that it is almost imposable to follow code when the namespace does not match the file structure.

 

I guess I don't have a definitive question here, but I am hoping to stimulate a discussion on this mater that I can join in on to get a better understanding as to how to approach namespaces with php.

 

                         -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

OOPS!

I'm sorry for posting this thread here. I didn't realize I was still in the Introductions section of the forum. Could someone please move this thread to the proper section and forgive my mammoth blunder here.

Edited by AncientTom
Link to comment
Share on other sites

The PHP standards explicitly demand that the namespace hierarchy correspond to the directory hiearchy, so PHP namespaces are actually very similar to Java namespaces. In Java, package resolution starts at the CLASSPATH entries; in PHP, there are base directories for different namespace prefixes. From that point on, both languages traverse the filesystem to find the classes (PHP uses autoloaders, to be exact).

 

If anything, PHP is more flexible. Yes, PHP programmers can violate the standards and make things confusing for everybody else. But I'd say that's the fault of the programmer, not necessarily a PHP problem.

 

By the way, the namespaces I've checked in the concrete5 respository did correspond to the directory structure. Are you sure you're reading this correctly? Can you give a counter-example?

  • Like 1
Link to comment
Share on other sites

The PHP standards explicitly demand that the namespace hierarchy correspond to the directory hiearchy,

... in PHP, there are base directories for different namespace prefixes. From that point on, both languages traverse the filesystem to find the classes (PHP uses autoloaders, to be exact).

If anything, PHP is more flexible. Yes, PHP programmers can violate the standards and make things confusing for everybody else. But I'd say that's the fault of the programmer, not necessarily a PHP problem.

By the way, the namespaces I've checked in the concrete5 respository did correspond to the directory structure. Are you sure you're reading this correctly? Can you give a counter-example?

 

I can't find the exact c5 files that exhibit the bad namespacing that I am referring to. I am remembering that the namespace in c5 that I was stumbling on was "Concrete\Core\Continuing\Path\To\Class". Before version c5.7 the file structure had a core sub-branch under concrete/ and this would have been a good namespace declaration. After the c5 make-over, they completely changed the file structure and eliminated the concrete/core/ sub-directory and redistributed references to core classes to the different blocks and packages. Although we can understand that this could happen when they carry over old controller classes for use in the new c5, this still makes it difficult to find things. I don't think that Eclipse with Java would let me get away with this kind of misuse of namespacing without re-writing the directory tree.

 

This brings to mind another question. That is, what is a good IDE to use with PHP. For years, I have just been writing PHP code in gedit and Filezilla to link to my remote web servers. I like to keep things simple. This has been working well for me up until now but I think that I now have to consider a good PHP specific IDE so that I can keep track of namespacing.

Link to comment
Share on other sites

There is no misuse. They're mapping the vendor prefix \Concrete\Core to the concrete5 base directory, which is perfectly fine. So \Concrete\Core\SomeSubnamespace\SomeClass can be found in

/path/to/your/concrete5/src/SomeSubnamespace/SomeClass.php

I understand that you expect a "Core" directory somewhere, but namespace resolution in PHP uses prefix mappings rather than simply prepending base directories like Java does. This may be unexpected at first, but it makes sense and is more flexible. I've always found those deep /path/to/basedir/org/vendor/foo/var/Class.class directories of Java rather stupid.

 

As to IDEs: Probably one of the best is PhpStorm, but it's fairly expensive if you don't have a sponsor. Decent free IDEs are Netbeans and Eclipse.

 

In any case, you should also set up a local test environment and use a version control tool like git or Mercurial to manage and deploy your code. Developing on a live server -- maybe even without backups -- is not really a good idea.

Link to comment
Share on other sites

package resolution starts at the CLASSPATH entries; in PHP, there are base directories for different namespace prefixes. From that point on, both languages traverse the filesystem to find the classes ...

 

There is no misuse. They're mapping the vendor prefix \Concrete\Core to the concrete5 base directory, which is perfectly fine. So \Concrete\Core\SomeSubnamespace\SomeClass can be found in

/path/to/your/concrete5/src/SomeSubnamespace/SomeClass.php

As to IDEs: Probably one of the best is PhpStorm, but it's fairly expensive if you don't have a sponsor. Decent free IDEs are Netbeans and Eclipse. In any case, you should also set up a local test environment and use a version control tool like git or Mercurial to manage and deploy your code. Developing on a live server -- maybe even without backups -- is not really a good idea.

 

I see. Thanks for that. That's what I am finding confusing. I have not yet run into the fact that a base namespace can be specified somewhere else or I missed it in the tutorials. I guess that it's equal to the package's namespace in Java. I will probably get a better picture of this when I get set up with Eclipse with PHP. I'll have to go back and review that to fix this concept in my head. Where, in a PHP package, would be the proper place to declare the base namespace and where would I expect to find this declaration in the new c5?

 

As far as backup of a CMS goes, just keeping your work local and uploading finished work to the server doesn't really backup the website. My understanding is that C5 is constructed with an on-line operation in mind. The website itself is not ready for viewing until the theme is complete. There is the concrete core space that you do not touch. You create your added work as a block in the application space and then add it to the view using the c5  dashboard. Content at many levels can be constantly changing at many levels on the website, all stored in a database as well as all of the metadata that is required to perform CMS operations. C5 is designed with it's own version control and works wonderfully. Rolling back is easy. I find that it is easier to design on the website in c5's background and then publish to the public when it is ready for viewing. I then download a copy of my work after publishing. Database backup is done automatically at regular intervals. Anyway, this has been my approach this far. I am always open to suggestions.

Link to comment
Share on other sites

Like I said, namespace resolution in PHP is performed by autoloaders. The autoloading process in concrete5 is explained in the manual, and the specific autoloaders are registered in the bootstrapping script.

 

That's not necessarily the prettiest option. PHP itself comes with a default autoloader which uses the include paths as the base directories -- very similar to Java. The package manager Composer which many projects use also comes with a customizable autoloader.

 

 

 

C5 is designed with it's own version control and works wonderfully.

 

I'm talking about actual programming, not content or layout changes.

 

When you write a significant amount of code, you should use source control to keep track of the exact changes, go back and forward in the history, create separate branches for new features instead of putting all changes into the main project etc. Personally, I use this even for small, private applications, because it saves a lot of time and trouble.

 

There are plenty of graphical tools, and the common version control systems like git are integrated into IDEs, so you use them intuitively after a short while.

Link to comment
Share on other sites

The autoloading process in concrete5 is explained in the manual, and the specific autoloaders are registered in the bootstrapping script.  PHP itself comes with a default autoloader .... The package manager Composer which many projects use also comes with a customizable autoloader...

 

I'm talking about actual programming, not content or layout changes.

 

When you write a significant amount of code, you should use source control to keep track of the exact changes, go back and forward in the history, create separate branches for new features instead of putting all changes into the main project etc. Personally, I use this even for small, private applications, because it saves a lot of time and trouble.

 

There are plenty of graphical tools, and the common version control systems like git are integrated into IDEs, so you use them intuitively after a short while.

 

I downloaded the PDT version of Eclipse yesterday and will be setting it up today. I am sure that when I start using this IDE, it will guide me into a more proper way of working, especially when git and github are natural extensions to the Eclipse environment. And, yes, I do concede that I will put git to use when writing my own blocks and modules.

 

There is a lot of new stuff to learn in the new c5 design and sometimes it is not to easy to find the right documentation that you need on the c5 website. While they have redesigned their documentation and search engine, it's still not of Google caliber so thanks for all the links. I will be looking at these pages.

Link to comment
Share on other sites

This link might help you out -> http://phpenthusiast.com/blog/how-to-autoload-with-composer

 

HTH John - I know it did me, for before this I was using my own crazy autoloader and it should make my GitHub repositories more standardized. 

Thanks, I'll look at that too. Tornados came through the county I live in and caused me to be without power for 3 days. I am still finding things that went bad when the power went out and need attention. It will be a while before I am back on my computer, one of which is my computer. It's not quite right after the power outage.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.