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

Ruslan Karimov:
Using custom types in Symfony & Doctrine
Sep 08, 2017 @ 16:30:26

Ruslan Karimov has written up a post for the 4xxi.com blog covering the use of custom types with Symfony and Doctrine to implement features specific to your database of choice.

ORMs are great. In theory, they insure you against potential changes in your RDBMS and offer an easy plug’n’play solution for integration your domain code with database. In practice, however, since ORMs have to be compatible with all major vendors, their possibilities are often limited. Perks and tricks of specific RDBMS are left out.

One of such perks is so-called range types in PostgreSQL. There are plenty of cases when range types are applicable, but people (in my experience) often stick to old solution with two separate DATETIME columns. Last time I asked a fellow developer about the reason behind it and they said: ‘I use Doctrine in my project, and Doctrine does not support it’. So here we are.

He starts with a brief introduction to a custom type in Doctrine and a ranged type in PostgreSQL. He then shows how to create a custom "DateReangeType" to work with the ranged types and how add it to your Symfony configuration as a custom type. He ends the post with an example of it in use in an entity and in the client code making the database request. Full code can be found in this Github repository.

tagged: custom types symfony doctrine tutorial database postgresql range type

Link: https://blog.4xxi.com/using-custom-types-in-symfony-doctrine-f865c7072757

SitePoint PHP Blog:
PHP Macros for Fun and Profit!
Mar 21, 2016 @ 18:47:17

On the SitePoint PHP blog they've posted another tutorial from Christopher Pitt, this time about macros in PHP, and how you can use the Yay library to add in custom pre-processed macros to your code.

I get really excited when developers feel empowered to create new tools, and even new languages with which to solve their problems. You see, many developers come to PHP from other languages. And many PHP developers can code in more than one language. Often there are things in those languages — small syntax sugars — that we appreciate and even miss when we’re building PHP things.

Adding these to a language, at a compiler level, is hard (or is it?). That is unless you built the compiler and/or know how they work. We’re not going to do anything that technical, but we’re still going to be empowered.

He starts off by describing the goal: a simple "range" macro that creates an array and fills it with integers. He helps you get the library installed and shows how to use it to pre-process a file and output the PHP version. He shows how to create the syntax for the macro in the format Yay is expecting for the array_slice shortcut. He also includes handling letting you slice out a portion of an array using the same notation. Finally he shows the resulting code after the pre-processing has happened and the macros have been resolved.

tagged: macro library yay tutorial range integer string array

Link: http://www.sitepoint.com/php-macros-for-fun-and-profit/

SitePoint PHP Blog:
Re-Implementing the Range Operator in PHP
Mar 09, 2016 @ 18:21:08

The SitePoint blog has a new post that revisits a previous tutorial about implementing a custom operator - the "range" operator - natively in PHP itself. In this new article (again, a repost from the article by Thomas Punt) he comes back and refines the implementation a bit to fix some of the issues with the previous version.

In the prequel to this article (hint: make sure you’ve read it first), I showed one way to implement a range operator in PHP. Initial implementations, however, are rarely the best, and so it is the intention of this article to look at how the previous implementation can be improved.

He starts by mentioning some of the drawbacks to the previous implementation, mostly that the computation of the value would only happen at runtime (slower) and not compile time. He then goes through the code from before and shares the updates to make that allow this compile-time option to work on the lexer, parser and adding in new code for the compilation stage. Finally he makes the updates the Zend VM to allow for these changes to take effect.

tagged: reimplement range operator tutorial source update

Link: http://www.sitepoint.com/re-implementing-the-range-operator-in-php/

SitePoint PHP Blog:
Implementing the Range Operator in PHP
Mar 07, 2016 @ 18:55:47

The SitePoint PHP blog has a new tutorial posted (a repost from this article used with permission) about implementing a new operator in the PHP core language: a "range" operator. This operator allows the definition of a range of values (integer/float) as an internal PHP representation.

In the post below, Thomas Punt implements the range operator in PHP. If you’ve ever been interested in PHP internals and adding features to your favorite programming language, now’s the time to learn! This article will demonstrate how to implement a new operator in PHP. The following steps will be taken to do this: updating the lexer, updating the parser, updating the compilation stage and updating the Zend VM. This article therefore seeks to provide a brief overview of a number of PHP’s internal aspects.

He starts with a look at the range operator and how the intended functionality would work (including when the errors would be thrown). He then goes through the steps listed above and makes additions to the source, complete with the C code to make each change. The article is not only a good look at how to add a custom operator but also gives a good overview of the internals of PHP and how things fit together.

tagged: range operator implementation language c thomaspunt tutorial

Link: http://www.sitepoint.com/implementing-the-range-operator-in-php/

Sameer Borate:
Period: Time range API for php
Nov 05, 2014 @ 16:55:28

In his latest post Sameer Borate looks at a library he's recently found that's helpful for working with dates and times, even easier than the DateTime handling built into PHP. The Periodlibrary, part of The League of Extraordinary Packages, aims to "resolve many recurrent issues around time range selection and usage."

Date/time programming is one of the tricky aspects of software development. Although inherently not complex in itself, coding date/time algorithms can be a subtle source of bugs. Especially in web development a feature such as payment subscription processing that ranges from days to weeks to months can get complex quickly. Also such kind of scenarios require additional features like auto renewal, scheduled email alerts to subscribers etc. Such kind of features require good date/time handling algorithms and libraries that handle such chores are always welcome. One such library I encountered recently is Period.

He walks you through the basics first - getting the library installed and creating a new instance of the class to work with. He goes through each of the methods available including the constructor, getting the duration between times and getting the start/end values back as DateTime objects. He also looks at the methods that allow you to create the ranges from various time frames (quarters, weeks, etc), compare ranges and modify time ranges that already exist.

tagged: time range library period leagueofextraordinarypackages datetime

Link: http://www.codediesel.com/algorithms/period-time-range-api-for-php/

Zend Developer Zone:
A little fun with the average tech salaries as reported by dice.com
Feb 14, 2011 @ 20:53:17

On the Zend Developer Zone, Cal Evans has had some fun with salaries - specifically the salary information recently posted by Dice.com.

Every year dice.com puts together a salary survey for those of us working in tech. It's a chance to look at what other developers say they are making and either snicker or sigh wishfully. This week they released "2010-11 Tech Salary Survey Results". [...] To me, page 5 was the most interesting. Page 5 is a table of salaries by metro area.

He's created a table to lay out the data in a bit more readable (and normalized) format with Charlotte, North Carolina being the baseline and other cities diverging up and down (in salary range) from there. It helps you compare both the salary range and the normalized version of it that includes some of the cost of living that comes along with the location.

tagged: salary range dicecom technology city table normalized

Link:

Dagfinn Reiersol's Blog:
Real programming with PHP 5.3 (part 4): A more realistic example
Jun 05, 2009 @ 12:57:21

On the PHP in Action blog Dagfinn Reiersol continues his look at real programming examples using the upcoming PHP 5.3 release. In this new tutorial he focuses on making a classed based on the Range design pattern to paginate results.

A Range object is defined by just two values (three in my variation), so it might seem like too much to have an object just to keep these values, but as you can see from the example, a Range class can have behaviors to change and compare ranges.

He gives two code snippets - the first defines most of the methods of the class in the constructor (using the lambda functions) and the second uses a __call to catch the calls to the same methods but defined normally as private (making them inaccessible to the outside world).

tagged: lambda designpattern range tutorial

Link:

Paul Gregg's Blog:
PHP algorithms: Determining if an IP is within a specific range.
Apr 28, 2009 @ 14:25:39

Paul Gregg has shared a method he's come up with to determine if a given IP is within a selected range.

Unfortunately although people usually understand that an IP address is simply an unsigned 32 bit integer, and is easily determined, usually with $_SERVER['REMOTE_ADDR'], where the real challenge is - is in specifying the range within which they wish to check that IP address. IP ranges are usually specified in three common ways (in increasing complexity): wildcard, start-end range, classless inter-domain routing.

He looks at each method and includes descriptions and code examples as well as a link to the source code and a live demo of it in action.

tagged: algorithms ip address specific range three method wildcard cidr startend

Link:

DevShed:
Expanding the Application Range of Visitor Objects in PHP 5
Aug 09, 2006 @ 11:57:01

DevShed continues their series on the Visitor objects in PHP5 with this second part, looking at expanding the "application range" of the objects.

Well, at this stage I'm assuming that creating visitor objects with PHP isn't a strange concept to you any longer, therefore I'll continue this journey by developing some additional -yet useful- examples on how to build up and implement visitors with PHP 5. The main purpose of this is simply reaffirming the concepts deployed in the first tutorial, so you can have a solid background for including your own visitor classes inside your PHP applications.

They start off by creating a new abstract class to use in this part, a User class with plenty of properties and methods. Next up, they make a class extending this base, abstract class to make a SoftwareUser that will accept visitor objects. They then use this interface and create a Visitor class to make the calls, showing a code example of them playing nicely together.

tagged: visitor application range tutorial php5 object visitor application range tutorial php5 object

Link:

DevShed:
Expanding the Application Range of Visitor Objects in PHP 5
Aug 09, 2006 @ 11:57:01

DevShed continues their series on the Visitor objects in PHP5 with this second part, looking at expanding the "application range" of the objects.

Well, at this stage I'm assuming that creating visitor objects with PHP isn't a strange concept to you any longer, therefore I'll continue this journey by developing some additional -yet useful- examples on how to build up and implement visitors with PHP 5. The main purpose of this is simply reaffirming the concepts deployed in the first tutorial, so you can have a solid background for including your own visitor classes inside your PHP applications.

They start off by creating a new abstract class to use in this part, a User class with plenty of properties and methods. Next up, they make a class extending this base, abstract class to make a SoftwareUser that will accept visitor objects. They then use this interface and create a Visitor class to make the calls, showing a code example of them playing nicely together.

tagged: visitor application range tutorial php5 object visitor application range tutorial php5 object

Link:


Trending Topics: