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

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

Nikita Popov:
The case against the ifsetor function
January 13, 2014 @ 09:22:52

In his latest post Nikita Popov aims to make a case against the introduction of the "ifsetor" function to be introduced into the PHP language. This function takes in a variable to find and, if found returns it. If not, it doesn't produce an error (or warning).

Recently igorw wrote a blog post on how to traverse nested array structures with potentially non-existing keys without throwing notices. The current "idiomatic" way to do something like this, is to use isset() together with a ternary operator. [...] Someone on /r/PHP pointed out that there is an alternative approach to this problem, namely the use of an ifsetor function.

He goes on to talk about by-reference argument passing, why requesting an undefined array index doesn't really throw an error and how writes don't have the same issues as reads. He then gets into his own issues around the "ifsetor" function, namely:

  • Creation of dummy values
  • No notices for nested indices
  • Null values treated as non-existing
  • Default is always evaluated
  • By-reference passing often forces a copy

He summarizes most of the issues in one statement - "there is way too much by-ref magic involved". He then looks at some of the ways that this could be helped but opts instead for something more like "get_in" as proposed by Igor.

0 comments voice your opinion now!
ifsetor getin array read write problem byreference

Link: http://nikic.github.io/2014/01/10/The-case-against-the-ifsetor-function.html

PHPMaster.com:
Say Hello to Boris A Better REPL for PHP
April 02, 2013 @ 10:34:00

On PHPMaster.com today Shameer C has a new tutorial introducing you to Boris, a REPL (read-eval-print loop tool) that's a bit more enhanced than the basic PHP interactive shell.

As web developers, we know the importance of the JavaScript console provided by the browser in testing out code snippets. We don't need to write an entire HTML page and JavaScript code just to verify the functioning or logic of a small routine we wrote. Instead, we simply run the expressions in the console and immediately see the results. Similarly, a REPL (Read-Eval-Print Loop) is the console of a programming language in which we can write code line-by-line and see what it does. [...] PHP's REPL is very good in what it does, although it does have some limitations. [...] And so, Boris tries to solve these problems and other concerns as well.

He walks you through the installation (via a git clone and, later, through Composer) and shows how to run it as well as some sample output. He also shows how to make a custom command-line Boris runner and how to embed it into your application. His example of a tool that would benefit from this is a command-line web service client using Boris and Guzzle.

0 comments voice your opinion now!
boris repl read eval print loop tool commandline github


MaltBlue.com:
5 Reasons Coding Standards Are Essential
March 13, 2013 @ 10:13:59

Matthew Setter has posted five reasons why he thinks that making a coding standard is an essential part of your development process. He suggests that "pain avoidance" is one of the key factors, both for new members of the team and for those maintaining it in the future.

Whenever you're working on a project, are you consistent? Are you consistent in your coding style, consistent in your documenting, consistent in your database naming conventions? Better yet, do you and your team have a coding standard which you consistently adhere to? If you don't, you're buying yourself and others a world of pain - which is painlessly simple to avoid. Today I'm banging the drum, shouting from the street corner, calling from the cathedral spire, imploring you to do one thing, above all else - pick a coding standard and then BE CONSISTENT!

His five reasons for implementing (and effectively using) a coding standard are:

  • Poor, Inconsistent Code - Causes You Pain
  • Your Code is Easier to Read
  • Your Code is Easier to Understand
  • Your Code is Easier to Maintain
  • Your Code is Easier to Collaborate on

Check out the post for summaries of each point.

0 comments voice your opinion now!
coding standard essential opinion maintenance read understand collaborate


Derick Rethans:
Read Preferences wth the MongoDB PHP driver
December 20, 2012 @ 13:41:24

Derick Rethans has a new post to his site detailing some of the "read" preferences that you can customize in the latest versions of the MongoClient functionality in the MongoDB PHP extension for replica sets and sharing setups.

Read Preferences are a new Replica Set and Sharding feature implemented by most MongoDB drivers that are supported by 10gen. This functionality requires MongoDB 2.2. In short, Read Preferences allow you to configure from which nodes you prefer the driver reads data from. In a Replica Set environment it is the driver that does the selection of the preferred node, and in a Sharded environment it is the mongos process that routes queries according to the defined Read Preferences.

He starts with a look at the read preference types (like "primary", "secondary" and "nearest") how the connection manager works to handle each type. He includes some code samples showing how to configure your MongoClient connections to use these various types of preferences. He also introduces the concept of "tags" for the replica set - aliases to make them a bit easier to identify when making a connection and how to define them in the connection string.

0 comments voice your opinion now!
mongodb mongoclient read preference replicaset sharding connection tutorial


Eric Holk:
How Do We Read Code?
December 19, 2012 @ 10:36:28

There's an interesting post on Eric Holk's blog talking about how we read code - a look at the results from a psychology experiment that tracked the viewer's eye movement as they scanned through code (complete with video).

The goal is to figure out some way of measuring what features in programming systems help programmers understand wht they are doing, and how this can be used to make systems that lead to higher quality software. Mike is currently running an experiment where he shows people several short Python programs and asks them to tell the output of the program. The test subject is sitting in front of an eye tracker, so afterwards Mike can see where you were looking at various times during the experiment.

The results are pretty interesting and Eric likens it to a sort of "just-in-time compilation" that the mind is doing as it reads through the code, not a straight forward read through. The timing of the read is interesting too, noting that once something is figured out, it's run through faster the following times.

One aspect he's interested in is how the approach of inexperienced programmers differs from that of experienced programmers. For example, there seems to be some evidence that following variable naming conventions helps experienced programmers understand the code much quicker, while breaking these conventions leads to a severe penalty. On the other hand, inexperienced programmers seem to take about as long regardless of how the variables are named.

This study is still going on and, if you're in the Bloomington, Indiana area and would like to lend your eyes to the cause, send an email over to Mike Hansen (more on the subject on his blog here).

0 comments voice your opinion now!
ericholk mikehansen read code psychology experiment video eye tracking


ZetCode.com:
PostgreSQL PHP Tutorial
May 07, 2012 @ 11:14:40

On the ZetCode.com site there's a five part tutorial posted about getting your PHP application up and running on a PostgreSQL database (updated on the 4th).

This is a PHP tutorial for the PostgreSQL database. It covers the basics of PostgreSQL programming with PHP. The examples were created and tested on Linux. [...] PostgreSQL is a powerful, open source object-relational database system. It is a multi-user, multi-threaded database management system. It runs on multiple platforms including Linux, FreeBSD, Solaris, Microsoft Windows and Mac OS X. PostgreSQL is developed by the PostgreSQL Global Development Group.

The chapters guide you through every step you'll need:

0 comments voice your opinion now!
postgresql tutorial introduction read images metadata transactions


Anthony Wlodarski's Blog:
Node.js and Zend Auth with Sessions stored in the database
March 07, 2012 @ 09:50:46

Anthony Wlodarski has posted a quick example of how he shared the sessions from Zend_Auth in his Zend Framework application over with a Node.js server/application.

Recently on a project I had to make changes to a underlying portion of the sites architecture to move sessions in Zend Framework from file storage to database storage. However this affected a piece of the architecture. Node.js, which manages all our real time interaction, looked at sessions at the file level. This was quite a easy transition for the function as it was abstracted away in a function call so the theory was to just replace the function "guts" with a new component.

The post shows the code he came from (which pulled in the PHP session file and extracted the session data manually) over to a new database-based version that selects from the SESSIONS table and pulls out the data. It's based on the table having an "id" column and the Zend_Auth namespace it uses.

0 comments voice your opinion now!
nodejs zendauth sessions platform read tutorial database


DevShed:
Handling File Data with PHP (Reading & Seeking)
December 01, 2011 @ 10:09:14

DevShed has posted the fifth part of their "handling files in PHP" tutorial series today (excerpted from the book "Beginning PHP and PostgreSQL 8: From Novice to Professional") with a focus on read from multiple places in a file to get to different data.

In this fifth part of an eight-part series on working with file and operating systems with PHP, you'll learn how to read data from a file and move the file pointer.

Example code is included showing how to use the file, file_get_contents, fgetc, fgetcsv, readfile and fscanf (among several others). Also included is code showing how to use the fseek method to move around in a file.

0 comments voice your opinion now!
tutorial file handling read seek move


Gonzalo Ayuso's Blog:
Checking the performance reading arrays with PHP
August 16, 2011 @ 11:46:07

While admitting that micro-optimizations aren't usually worth the time that's taken to worry about them, Gonzalo Ayuso has thrown together some array read benchmarks to show the difference, if any, in where array values are fetched.

Normally our code is coded once and executed thousands of times, and we must keep in mind that CPU time many times means money. We need to balance it (as always). But, WTH. I like micro-optimizations, and here comes another one: Checking the access to arrays with PHP.

He sets up three different options and tests the memory consumption and run time for each:

  • Referencing a value from a large array outside a for loop
  • Referencing a value from a large array inside a for loop
  • Echoing out the value from a large array inside a for loop

Not surprisingly, all three approaches yield just about the same results. It probably has more to do with the size of the large array than how it's accessed. The fetch outside the for loop did come in slightly under the others, but not enough to worry about it.

0 comments voice your opinion now!
performance array microoptimization read memory runtime



Community Events





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


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

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