News Feed
Sections




News Archive
feed this:

Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Rob Allen:
Use statements
March 17, 2014 @ 10:13:08

Rob Allen's latest post focuses in on something that's been a part of PHP for a while now, back when namespacing was introduced - the "use" keyword. He shares some thoughts, both from others and himself, about whether or not they make code more readable.

I was having a discussion on IRC about use statements and whether they improved code readability or not. [...] Those longer class names make it a little hard to quickly parse what it going on. The [example with "use" statements] is clearly less cluttered, but is at the expense of ambiguity. Exactly what class is User? I would have to go to the top of the file to find out. Should I use aliases? If so, how should I name them?

He went out to Twitter for advice from other PHP developers on the issue too. The feedback from his question came mostly in support of the "use" statements:

  • "I think use statements just abstract where the class is coming from. Some people find that useful."
  • "I think it's helpful seeing all of the packages used by a class without having to look through the full code."
  • "One reason I like them is that I can glance at a file and know dependencies immediately."
  • "I do appreciate what you are saying about the indirection use statements introduce."

There's also a bit of talk about "aliasing" with namespaces rather than the full classname, then using the namespace and class name in the code to "minimise ambiguity".

0 comments voice your opinion now!
use statement namespace twitter advice feedback alias

Link: http://akrabat.com/php/use-statements/

Reddit.com:
I do not get how to use a php framework.
January 16, 2014 @ 10:37:34

If you're new to the world of PHP frameworks and are having trouble wrapping your head around using one, you should check out this discussion over on Reddit. The poster asks some advice on getting started with frameworks and wether they need one or not.

Hey. I've been learning PHP now for some weeks on a hobby level and I do think it's alot of fun! But, I hear, everywhere people say "Dont reinvent the wheel" and how good frameworks are, but there is some stuff stopping me. [...] And why I just do not keep continue to do my own buisness is becasuse, I hear all this talk about how secure frameworks are, and Im super scared of writing a php website and messing something up. SQL Injections etc etc. So... Do I REALLY need to use a framework, It seem to just complicate things to a whole other level than my current PHP skill.

Responses on the post include a good range of thoughts including:

  • "You could instead use a CMS which have elements of a framework built in"
  • "I think that Silex is just right for you, and should be familiar enough with your current practice while teaching you the new things you need to know as you go along."
  • "Absolutely use a framework, but don't lose that drive to learn how code works."
  • "Any framework of repute is making heavy use of design patterns. Having a rudimentary understanding of these will help frameworks make much more sense to you."
  • "A framework exists to ease your development [...] A lot of sites use frameworks, but there's nothing stopping you from doing your own thing."
  • "If you get confused by larger frameworks and like to have more control over every aspect of your code, it sounds like a micro-framework might be the right choice for you."
0 comments voice your opinion now!
use framework opinion beginner introduction

Link: http://www.reddit.com/r/PHP/comments/1va2dn/i_do_not_get_how_to_use_a_php_framework/

Blake Gardner:
Practical usage of PHP 5.5 generators yield keyword
June 24, 2013 @ 11:54:42

With the release of PHP 5.5 came a whole group of new features, including the "yield" keyword for better handling of values in iteration. Blake Gardner has posted a practical example of its use to his site today.

The key to understating the way the yield works verses a normal function is that rather than generating all of your data and returning the final array when it's done; you yield the value as it's generated. The state of the generator function is saved after you yield and then its state is restored when called again so the iteration can continue.

He shows a basic use of "yield" in a simple foreach of 1000000 values. In the first example, memory is exhausted and the second yields the values as they come, reducing the overhead significantly. The "range_yield" function returns them as the "for" loop generates them.

0 comments voice your opinion now!
yield feature practical use tutorial generator

Link: http://blakegardner.co/2013/06/24/practical-usage-of-php-5-5-generators-yield-keyword

Gonzalo Ayuso:
Multiple inheritance with PHP and Traits
December 19, 2012 @ 13:17:48

Gonzalo Ayuso has a new post today showing how you can use traits in PHP to simulate a kind of multiple inheritance.

Multiple inheritance isn't allowed in PHP. [It's not] possible with PHP (in Java is not possible either), but today we can do something similar (is not the exactly the same) with Traits. Let me explain that: Instead of classes we can create Traits.

He includes a code example showing the creation of two traits, "Base1" and "Base2", that are implemented (via "use") and the calls to methods on each. He also points out the error condition and message that can come up when there's a conflict in the method names between two or more traits. This is relatively easy to solve with the mapping ability of the "use" statement (code example included for that too).

0 comments voice your opinion now!
multiple inheritance traits python example mapping use


Josh Adell:
Interfaces and Traits A Powerful Combo
September 28, 2012 @ 08:51:16

Josh Adell has a new post today looking at the "powerful combination" of using traits and interfaces in PHP applications. He shows how, despite traits not implementing the interface directly, they can be used to make other classes adhere to them simply by "using" them.

If you're not using interfaces in PHP, you are missing out on a powerful object-oriented programming feature. An interface defines how to interact with a class. By defining an interface and then implementing it, you can guarantee a "contract" for consumers of a class. Interfaces can be used across unrelated classes. And they become even more useful when combined with the new traits feature in PHP 5.4.

He illustrates with a package shipping example and uses an "Addressable" Interface to define the structure for both a Company and Users class. He includes code showing how to implement it in a more traditional "implements" way in a class, but also shows an interesting way to achieve the same thing with traits. Having a trait that follows the interface makes it easy to have a class adhere to the interface just by including the trait (or "using" it).

0 comments voice your opinion now!
interface trait tutorial implement use structure


Kevin Schroeder's Blog:
10 reasons to use PHP
May 31, 2012 @ 08:47:14

Kevin Schroeder, in his move towards doing some mobile development, has a new post to his blog about why he's still going to stick with PHP for the backend of this new development work.

I do like working with client/server-like architectures and so I intend to be building apps that have a fair amount of server-side processing to back it up. More details on that to come in the next few months. [...] I have decided that, for the time being, to use PhoneGap for my frontend development. [...] So the question was what to use for the backend development and, to nobody's surprise I presume, PHP is my chosen way to go.

Some of his reasons for the choice include:

  • PHP is stupid easy to scale
  • It is tied to the web
  • (Available) Frameworks
  • Tons of blogs
  • It integrates with everything

Check out the post for more of his reasons.

0 comments voice your opinion now!
reason use opinion mobile development phonegap


Rob Allen's Blog:
A primer on PHP namespaces
February 16, 2012 @ 08:25:43

For those that either haven't worked much with PHP 5.3 in their applications (or just haven't gotten around to using the feature) Rob Allen has put together an introduction to namespaces to guide you through some first steps and share some example usage.

I know that there are a lot of posts now about namespaces in PHP 5.3. This is mine which is how I learnt how they work. [...] That is, namespaces allow us to: combine libraries with the same classnames, avoid very long classnames and organise our code easily. Note that namespaces do not just affect classes. They also affect functions and constants.

He starts with the basic namespace definition (using the "namespace" keyword), shows how to import another namespace with "use" and the use of the __NAMESPACE__ constant to determine what namespace you're operating in. More information on namespaces can be found in the PHP manual.

0 comments voice your opinion now!
namespace introduction tutorial constant use


Query7.com:
Why You Should Be Using A PHP Framework
April 11, 2011 @ 09:58:14

On the Query7.com blog, Logan has posted his opinion on how you should be doing your development on sites that are more than just one or two pages - you should be using a framework.

Frameworks should be used when constructing web applications. Any application that involves a database, forms, sessions, cookies or a remote service (such as Twitter or Facebook) will benefit from being powered by a framework. There is no need to use a framework for a website that has only one or two pages, nor for command line utility scripts.

He lists some of the common features frameworks provide including database abstraction, caching, form management, authentication and internationalization. He also includes some of the more general benefits you get from using frameworks like portability, shorter development time, application security, plugins/module support and the enforcement of good coding standards (depends on the framework, obviously).

0 comments voice your opinion now!
framework opinion use feature benefit


Propel Blog:
The End of Autoloading
March 25, 2011 @ 11:13:51

On the Propel blog there's a recent post talking about how the age of autoloading might be ending and how namespacing could be the next logical step (or could it).

Autoloading in PHP is a great time saver. It lets you write concise scripts without the knowledge of the exact directory structure of the libraries you use. But with the arrival of namespaces in PHP 5.3, and the influence of Java over new generation PHP frameworks, autoloading is changing. In the near future, explicit autoloading will be ubiquitous, but with none of the advantages of the old style autoloading.

He talks about "the old days" when things were included manually through file paths, how that graduated to the SPL autoloading and, most recently, up to namespace autoloading. He shares code samples of how the namespace loading works and how you can abuse it to override current classes/functionality with your own. He points out one interesting correlation though - that the "use" keyword seems a lot like the "require_once" of way back when. He shows how the added verbosity of namespace usage can be a hinderance on frameworks, citing microframeworks specifically and showing one implementation that's non-namespaced next to another that is.

0 comments voice your opinion now!
autoloading namespace requireonce use spl


Evert Pot's Blog:
Taking advantage of PHP namespaces with older code
February 01, 2011 @ 10:10:35

Evert Pot has a quick post about a suggestion mentioned at PHPBenelux related to using namespaces with older code.

If you're running PHP 5.3 and you have to use pesky old code that uses long class prefixes (yea, so, pretty much all PHP code out there), you can still make use of namespace features to shorten them.

He includes a quick example that shows the shift from using the traditional Zend_Controller_Action_Helper_AutoComplete_Abstract to an aliasing with the use/as to just reference it as AutoComplete.

0 comments voice your opinion now!
namespace old code zendframework use keyword



Community Events





Don't see your event here?
Let us know!


symfony release community interview library opinion podcast deployment laravel bugfix framework tips introduction zendserver unittest language package api series install

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework