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

NetTuts.com:
Refactoring Legacy Code Part 2 - Magic Strings & Constants
April 03, 2014 @ 12:47:46

NetTuts.com has posted the second part of their "Refactoring Legacy Code" series today continuing on from their beginning of the series. They continue the refactor of their "trivia" application.

Old code. Ugly code. Complicated code. Spaghetti code. Jibberish nonsense. In two words, Legacy Code. This is a series that will help you work and deal with it. We first met our legacy source code in our previous lesson. [...] The time for the first changes have come and what better way to understand a difficult code base than start to extract magic constants and strings into variables? These seemingly simple tasks will give us greater and sometimes unexpected insights into the inner workings of legacy code. We will need to figure out the intentions of the original code author and find the proper names for the pieces of code that we've never seen before.

They talk about refactoring out things like "magic strings" and other hard-coded return values and checks. They mention updating the tests to reflect these changes while keeping an eye out for "magic constants" as well.

0 comments voice your opinion now!
refactoring unittest magic string constant trivia

Link: http://code.tutsplus.com/tutorials/refactoring-legacy-code-part-2-magic-strings-constants--cms-20527

The PHP.cc:
PHP 5.5 New CLASS Constant
June 26, 2013 @ 09:02:08

The PHP.cc have posted another article in their series looking at the new features that come with the latest release of PHP (5.5). In this new post they cover the "CLASS" constant.

Last week, the first stable version of PHP 5.5 was released. It introduced a class-level constant, aptly named CLASS, that is automatically available on all classes and holds the fully-qualified name of that class. [...] So why would you need such a constant? [...] When you need the fully qualified name of a namespaced class that is referenced by a namespace alias ... then it gets interesting.

He illustrates with an example of a unit test using stubs and mocks. The normal method requires the definition of the class namespace in the "getMock" call. With the CLASS constant, PHP can extract that information from the namespace referenced in the "use" and drop it in as a replacement.

0 comments voice your opinion now!
class constant php55 new feature namespace unittest

Link: http://thephp.cc/viewpoints/blog/2013/06/php-5-5-new-class-constant

Lee Davis' Blog:
The enum conundrum
July 06, 2012 @ 11:56:52

In a new post to his blog Lee Davis describes the enum conundrum - what's the right solution for effectively using ENUM-type fields in your data?

So a user signs up and I want to store a status that reflects their account, or at least an identifier representing that status. Their account could be active, disabled (temporarily), pending approval or maybe deleted. Should I use an enum? I've heard they're evil. Maybe having a reference table with statuses would be better? But now I have to manage a separate table just for that one snippet of data, is that overkill? Could I maybe use that status table for other entities? Or, could I instead just use an integer and reference it on the code level? What is the right solution?

He presents three of the most common situations he's seen for people using enums in the application:

  • "I used enums all over the place" (maintenance between code and DB values)
  • "use a reference table"
  • "I could use a class constant to represent the enum" (enforced in the app)

Of the three, he suggests the third as the option with the most advantages. Not only does it make it simpler to get the allowed values for the field, but you're also more flexible in the kinds of validation you can do on the values.

0 comments voice your opinion now!
enum conundrum reference table constant maintenance


Mike Purcell's Blog:
PHPUnit - Constant Already Defined - -process-isolation
June 07, 2012 @ 08:43:25

In this new post from Mike Purcell shares a solution to an error he came across in his unit testing - a "Constant already defined" message - and how he fixed it.

When upgrading the unit tests I had no problem, it was only when I was trying to integrate into CruiseControl that this was happening. Well after some time, I finally figured out the issue was the "ant" command to execute the unit tests was passing the -process-isolation flag to phpunit.

He found that when he called it with that flag, it was executed twice, resulting in the "already defined" message the second time. He fixed it by removing the extra flag. Another alternative is to wrap your constant definitions (and anything that dosen't need to be called/created again) in an "is defined" check.

0 comments voice your opinion now!
phpunit processisolation error constant


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


DevShed:
Effects of Wrapping Code in Class Constructs
December 29, 2011 @ 10:06:58

DevShed has a new tutorial posted today looking to help you counteract the bad practice of wrapping procedural code in "class" constructs and provide some useful suggestions of how to avoid it.

Static helpers seem to be a great idea at first glance, as they're reusable components that don't require any kind of expensive instantiation for doing common tasks [...]. But the sad and unavoidable truth is in many cases they're simply wrappers for procedural code, which has been elegantly hidden behind a "class" construct. So what's wrong with this? Well, even in the most harmless situations, when you use a static helper that produces a deterministic output, you're actually throwing away the advantages that OOP provides.

To illustrate, they create a basic validation class that can check for things like valid emails, float values, integers and URLs using PHP's filter_var function. They point out that the class is difficult to extend and that it is doing too many things to be correctly considered a "piece" of functionality. To correct the problem, they opt for a different approach - an abstract class acting as an interface to structure custom validators against. This provides set/get methods for things like the error message and value to evaluate. The implementation of the validators on top of this class is coming in the next part of the series.

0 comments voice your opinion now!
tutorial constant static method interface abstract class


Brandon Savage's Blog:
Custom Apps Some Strategies For Easy Configuration Files
September 16, 2009 @ 07:59:02

In this new post to his blog today Brandon Savage looks at configuration options for PHP apps and gives some examples to help you get the thought process started for your development.

One of the decisions that has to be made each time an application is written for distribution is how best to set up the configuration files. There are a number of different approaches taken to this: some opt to use the define() function and define constants, while others use large arrays.

He points out a few "overlooked options" that some developers might no consider when working with configuration files like class constants and ini files (which PHP can parse natively). A few code snippets are included to show examples of them in use.

0 comments voice your opinion now!
configuration class constant ini


Paul Reinheimer's Blog:
Memcached Constants - Lame Code
June 19, 2009 @ 10:27:26

If you've worked with memcached and have ever run into an error, you might get tossed a code, the value of the error's constant, rather than the actual constant name. Paul Reinheimer was seeing just that and decided to map out all of the constants in relation to their error codes for future reference.

I was running into a few errors, and getResultCode() was obligingly returning the value of the constant, rather than the constant itself. I had to look up what that value meant. For some reason I couldn't reflect the extension to get the constants and values thereof, so with some lame scraping you get the following.

He's included the list in both a normal text format and as a PHP array that can be cut and pasted directly into a library.

0 comments voice your opinion now!
code error constant memcached


Joseph Crawford's Blog:
MySpace PHP REST Library
April 30, 2009 @ 10:25:55

For those still looking to interface with MySpace, Joseph Crawford has pointed out an official REST library for PHP to connect with their API.

Over the last few days I have been digging deep into the concepts of REST. I have been reading through my copy of RESTful Web Services. Out of curiosity I searched Google for a "php REST library" and it turned up the Official MySpace PHP REST Library.

He points out one interesting thing about it, though - it's like they started using an object-oriented design but build after making the classes with properties. No methods.

Even if they were to choose to go the PHP 5 way I would highly suggest that they do not use classes as containers for constants, there is no need to do this. It is not using proper OOP methodologies. Why create an object that just holds 2 - 4 constant variables, these would be better suited for define() calls in my opinion.
0 comments voice your opinion now!
myspace rest library php5 oop constant library


Packt Publishing:
PHP Magic Features
April 14, 2009 @ 09:31:48

Packt Publishing has posted a new article from Jani Hartikainen about the "magic methods" that PHP comes with - methods, properties and constants really.

Magic methods, which are class methods with specific names, are used to perform various specialized tasks. They are grouped into two: overloading methods and non-overloading methods. [...] Magic functions, which are similar to magic methods, but are just plain functions outside any class. [...] Magic constants, which are similar to constants in notation, but act more like "dynamic" constants. We'll also look at some practical examples of using some of these, and lastly we'll check out what new features PHP 5.3 is going to add.

He looks at the various functions/methods and constants (like __clone, __toString), some of the overloading methods like __call, and magic constants like __FILE__ and __CLASS__. He wraps it up by briefly discussing what PHP 5.3 adds in - a few new magic methods and constants (but no functions).

0 comments voice your opinion now!
magic features php5 constant function method



Community Events





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


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

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