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

Mathias Verraes:
Named Constructors in PHP
June 13, 2014 @ 09:42:15

Mathias Verras has a new post to his site about an idea he calls "named constructors". This method uses static factory methods to simulate the idea of a constructor and initialize the object.

PHP allows only a single constructor per class. That's rather annoying. We'll probably never have proper constructor overloading in PHP, but we can at least enjoy some of the benefits. Let's take a simple Time value object. Which is the best way of instantiating it? The only correct answer is "it depends".

His example shows the typical constructor creation with variable arguments, but points out that this can get messy quickly. His other method, the factory methods as "constructors", can make for a cleaner interface and makes the class more flexible. They make the object able to be initialized with different types of values and even satisfies the Single Responsibility Principle. He goes through a few examples using his "Time" class, showing how different "constructor" methods can be used to handle inputs ranging from a normal hour/minute format out to a "from minutes since midnight" value.

0 comments voice your opinion now!
named constructor factory method static tutorial time

Link: http://verraes.net/2014/06/named-constructors-in-php/

LeaseWebLabs.com:
How to use the "yield" keyword in PHP 5.5 and up
May 23, 2014 @ 12:09:47

In a recent post to the LeaseWebLabs blog Maurits van der Schee looks at the use of the "yield" keyword in PHP 5.5 to work with generators. A generator is very similar to a function that returns an array, in that a generator has parameters, can be called, and generates a sequence of values but it yields values one at a time.

The concept of generators is not new. The "yield" keyword exists in other programming languages as well. As far as I know C#, Ruby, Python, and JavaScript have this keyword. The first usage that comes to mind for me is when I want to read a big text file line-by-line (for instance a log file). Instead of reading the whole text file into RAM you can use an iterator and still have a simple program flow containing a "foreach" loop that iterates over all the lines.

He includes a few code examples showing a class that can read in data from a file in chunks and output the lines as they're extracted (versus using something like file). He also talks about a small performance comparison in working with the file pointer, fread over fgets. He even makes a simple benchmark script to compare the overall time and memory consumption of the fetching of different byte "chunks" from the file.

0 comments voice your opinion now!
yield generator file read fread fgets memory time benchmark

Link: http://www.leaseweblabs.com/2014/05/how-to-use-yield-keyword-php

Lorna Mitchell:
PHP 5.6 Benchmarks
May 19, 2014 @ 09:32:18

Lorna Mitchell has put together a set of benchmarks for PHP 5.6 comparing them to the three previous minor versions, PHP 5.5, 5.4 and 5.3 based around the same setup as her previous benchmarks of PHP 5.4.

A while ago I did some benchmarks on how different versions of PHP perform in comparison to one another. This isn't a performance measure in absolute terms, this was just benchmarking them all on the same laptop while it wasn't doing anything else, and averaging the time it took to run the benchmark script. Recently I ran it again for versions PHP 5.3 through PHP 5.6 and I thought I'd share my results.

There's a steady drop in execution time over the series of versions, with PHP 5.6 coming in the shortest. She also includes the actual numbers of the results in case you'd like to chart them out yourself.

0 comments voice your opinion now!
php56 benchmark previous version execution time

Link: http://www.lornajane.net/posts/2014/php-5-6-benchmarks

SitePoint PHP Blog:
Time Complexity of Algorithms
May 14, 2014 @ 12:25:37

The SitePoint PHP blog has a recent post looking at time complexity in the algorithms you develop in your PHP applications and how that relates to "Big O notation". Big O notation is simply a way of expressing complexity and performance of a method in a less subjective way than "it's faster than.."

If you are a web developer or a programmer in general, you have most likely written algorithms for various tasks. [...] One specification of an algorithm is its correctness. You will probably assume that your algorithm works after testing it out a few times. However, if you can mathematically prove that your algorithm will work as expected for every input value possible, this is a huge bonus. I will not go further in to that subject in this writing. Another specification is its efficiency: how does the computing time relate to the amount of input? Is it a linear relation? Does computing time rise exponentially for the doubling of input? That's what this article will be about.

He starts by talking about the concept of "time complexity" and how it relates to the overall efficiency of the algorithm. He then gets into the definition and examples of Big O notation, including code showing O(n) and O(n2) methods. He talks some about inefficient and efficient algorithms and follows with a refactoring example of moving from one to the other.

0 comments voice your opinion now!
complexity algorithm bigo notation mathematics time tutorial

Link: http://www.sitepoint.com/time-complexity-algorithms/

Ben Ramsey:
Dates Are Hard
February 24, 2014 @ 09:03:24

In a new post to his site Ben Ramsey talks about why dates are hard and can be frustrating to work with sometimes. It revolves around an issue he recently found with calculating a time for a UUID and days of the week.

No, I'm not talking about a meeting with a lover or potential lover. While those can be stressful, the calendar math used to determine the precise date and time on which such a meeting might occur is infinitely more difficult to perform. To software programmers, this isn't news, but I recently encountered an issue when calculating the time for an RFC 4122 UUID that had me questioning the accuracy of our modern, accepted calendars, especially with regard to the days of the week on which our dates fall.

In his work on his UUID library, he came across a the bug because of some failing unit tests. It was only happening in certain versions of PHP and upon further investigation found the issue to be a wrong day of the week from a date in 1582 (the correct value being Sunday). As it turned out, the date in question was actually a Saturday and his local environment was reporting bad results. The problem was with a revision made to the Gregorian calendar, removing 10 days causing a difference between the Gregorian and Great Britain versions of 1582.

0 comments voice your opinion now!
date time calendar gregorian unittest greatbriain year

Link: http://benramsey.com/blog/2014/02/dates-are-hard/

NetTuts.com:
Dates and Time - The OOP Way
October 24, 2013 @ 11:49:23

On NetTuts.com today there's a new tutorial they've posted showing how to use PHP's DateTime functionality in a more OOP kind of way. The DateTime extension lets you work both ways - procedural and OOP, with only slightly different syntax changes between them.

The Date/Time PHP extension is a set of classes that allow you to work with almost all of the date and time related tasks. It's been available since the release of PHP 5.2 and the extension introduced several new classes.

The tutorial first shows you some of the differences between just working with something like date and DateTime. From there they get into a bit more complicated things like:

  • Modifying dates/times
  • Working with multiple dates
  • Working with timezones
  • Using DatePeriods
  • Extending the current functionality

There's also two more "real world" usage scenarios included - defaulting to using UTC times and using the DateInterval to handle subscription payment logic.

0 comments voice your opinion now!
datetime tutorial oop introduction date time

Link: http://net.tutsplus.com/tutorials/php/dates-and-time-the-oop-way/

Erika Heidi Reinaldo:
Productivity and The Pomodoro Technique
October 09, 2013 @ 09:26:24

While not specifically relating to PHP, Erika Heidi's latest post talks about a technique that could help you be more productive in the coding work you do - focusing in on your "time problems" and possibly using the Pomodoro technique to help correct them.

This is a quite famous quotation that is being repeated through the years. "Time is money" is a very contrived way to say that if you lose time, you might be losing money. I personally don't like this quote; lets refactor it to something that better reflects reality: "Productivity is Money" sounds way more realistic. [...] What we really need is to figure out a way to better use the time we have. How do we maximize our productivity?

She breaks it up into four things that can help identify these "time problems":

  • Diagnosing your time problems
  • Managing your Focus
  • Self-sabotage by the scumbag brain
  • The Pomodoro Technique

In this last section she introduces the technique (and tool - the tomato timer) and how it works. She points out the places where the process is flexible and how, especially if you're deep in code, getting to the point of taking a timed break (and sticking with it) can help give perspective.

0 comments voice your opinion now!
pomodoro development time management practice

Link: http://www.erikaheidi.com/2013/10/08/productivity-pomodoro-technique/

Gonzalo Ayuso:
Handling dates with PHP
October 02, 2012 @ 08:41:09

In this new post to his site Gonzalo Ayuso introduces you to one of the more powerful parts of the PHP language - the DateTime object.

I've seen a lot of newbies (and not newbies) having problems handling dates in PHP (and even with SQL and another languages). When I see someone having problems with dates, I always ask the same question. I type in a text editor "27/11/2012″ and I ask him: What is it? If your answer is "This is a date" you should continue reading the post.

He talks about how the DateTime functionality replaces (much more effectively) some of the older date handling methods in PHP. He includes a few examples comparing it to date and showing how it can be used to compare dates. He includes a "Dummy" class he mocked up to show how you could work with DateTime to get/set formatted dates, set the format to use and get the current format. As always, he also provides tests for the code as well.

This is just the tip of the iceberg as to what DateTime can do, so I'd suggest checking out the manual page for it to see the full list of features.

0 comments voice your opinion now!
datetime date time handling introduction formatting


Lorna Mitchell:
PHP 5.4 Benchmarks
July 19, 2012 @ 09:54:42

In this quick post to her site, Lorna Mitchell shares some of the benchmark results she found when doing some tests with the latest version of PHP - 5.4.

Today I'm giving my first ever talk at OSCON - about PHP 5.4 (I'll also be giving my second ever talk at OSCON, about RESTful services; it's a busy day!). My talk includes some benchmarks which I thought I'd also share here. [...] This graph shows the performance of four versions of PHP (because the bench.php script that lives in the php source tree didn't appear until 5.1). The axis up the left is the time it took to run the benchmark script - so a smaller number is better news.

You can see a dramatic difference between even just the latest in the PHP 5.3.x series in the 5.4 results. There's also a table with the details of each of her 10 executions of the "bench.php" script showing the results of the time spent to run the script on four different PHP versions.

0 comments voice your opinion now!
benchmark version execution time performance


PHPMaster.com:
Working with Dates and Times in PHP and MySQL
March 01, 2012 @ 08:51:47

On PHPMaster.com today there's a new tutorial by Sean Hudgston about working with dates and times via the PHP date functions and how they cooperate with dates/times from a MySQL database.

When working in any programming language, dealing with dates and time is often a trivial and simple task. That is, until time zones have to be supported. Fortunately, PHP has one of the most potent set of date/time tools that help you deal with all sorts of time-related issues: Unix timestamps, formatting dates for human consumption, displaying times with time zones, the difference between now and the second Tuesday of next month, etc. In this article I'll introduce you to the basics of PHP's time functions (time(), mktime(), and date()) and their object-oriented counterparts, and then take a look at MySQL dates and show you how to make them play nicely with PHP.

His examples include how to get the current Unix time, formatting dates/times, making timestamps and working with the more powerful DateTime objects. On the MySQL front, he shows the result of a normal date select, one using the "unix_timestamp" function and how to shift the result based on the user's timezone.

0 comments voice your opinion now!
date time mysql datetime tutorial format unix timestamp



Community Events





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


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

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