SitePoint PHPClean Code Architecture and Test Driven Development in PHP (8.2.2016, 18:00 UTC)

The Clean Code Architecture was introduced by Robert C. Martin on the 8light blog. The idea was to create an architecture which is independent of any external agency. Your business logic should not be coupled to a framework, a database, or to the web itself. With the independence, you have several advantages. For example, you have the ability to defer technical decisions to a later point during development (e.g. choosing a framework and choosing a database engine/provider). You can also easily switch the implementations or compare different implementations, but the biggest advantage is that your tests will run fast.

Just think about it. Do you really have to run through a router, load a database abstract layer or some ORM magic, or execute some other code just to assert one or more results?

I started to learn and practice this architecture because of my old favorite framework Kohana. At some point, the core developer stopped maintaining the code, which also meant that my projects would not get any further updates or security fixes. This meant that I had to either move to another framework and rewrite the entire project or trust the community development version.

Clean Code Architecture

I could have chosen to go with another framework. Maybe it would have been better to go with Symfony 1 or Zend 1, but by now that framework would have also changed.

Frameworks will continue to change and evolve. With composer, it is easy to install and replace packages, but it is also easy to abandon a package (composer even has the option to mark a package as abandoned), so it is easy to make “the wrong choice”.

In this tutorial, I will show you how we can implement the Clean Code Architecture in PHP, in order to be in control of our own logic, without being dependent on external providers, but while still using them. We will create a simple guestbook application.

Continue reading %Clean Code Architecture and Test Driven Development in PHP%

Link
Thijs FerynBastian Hofmann – Talking about ResearchGate, Berlin & PHPBenelux (8.2.2016, 09:46 UTC)

Bastian Hofmann My friend Bastian Hofmann was flying to Brussels to speak at the PHPBenelux Conference that I organize. I

The post Bastian Hofmann – Talking about ResearchGate, Berlin & PHPBenelux appeared first on Thijs Feryn's blog.

Link
nick halsteadThe technical reason Twitter cannot reorder Timeline (7.2.2016, 09:42 UTC)

I woke up to the news that Jack has denied that Twitter had plans to have a re-ordered timeline.

I was highly sceptical of that ever happening and it is for a purely technical reason. Now anything is of course ‘possible’ but Twitter has a particularly difficult problem when trying to do what Facebook does and it is the very reason Facebook sticks to 5000 friends.

Twitter has always allowed you to follow any number of people and over the years this has fundamentally been the core of most of it’s downtime, it is a very complex problem to deal with the idea that for every Tweet generated it must be distributed to potentially millions of users.

I don’t know the final solution Twitter used but I know it has taken many many years to evolve and develop bespoke software that can route Tweets between users who can have follow any number of other users, or be followed by any number of users.

And that is the problem, Facebook always kept a low limit because it’s fundamental design was that each time you refresh your timeline it basically does a complete re-render, I suspect that they rely upon the fact that they can first work out first a chronological list of updates from everyone you follow, and then they use the magic formula they use to re-order based upon your likes/priorities etc.

Twitter cannot do this – they allow you to follow an arbitrary number of other users – that means if you’re following 50,000 other accounts that means you need to do these calculations for every single update from those 50,000 accounts. If we assume every single account has Tweeted at least once (in last 24 hours) – that is 50,000 Tweets. I now must examine each Tweet, and rank it based upon it’s relevance to the user viewing – lets just look at what that ‘could’ involve.

  • Examine Tweet for subject matter (this could be pre-calculated)
  • Is the subject of interest to the user viewing (could be done by pre-caching subject relevance values)
  • Is the Tweet written by someone of importance? (remember Klout?)
  • Is the Tweet written by someone I interact with regularly (calculated either by number of @ mentions / or ‘retweets’ etc)
  • Does the Tweet contain a link that is important (based upon trending data – this is also likely cached)

Imagine the above 50,000 times for every time you want to view the timeline. Of course all things technical can be achieved – but Twitter has had 5+ years of making the current (very difficult) solution work, and how it currently works and how a re-ordered timeline needs to be calculated are not compatible in any shape or form.

 

 

 


Link
Michelangelo van DamPHP Arrays - The php array functions (6.2.2016, 16:13 UTC)
Source: Xavier33300 on Flickr.com
In my previous article about arrays (the basicssimple operations and associative arrays or hash maps) I have shown what arrays are and what you can do with it. Now I want to dive into real fun and explain PHP's array functions with real-world examples and how you can apply them in your day-to-day work.

Collection to select form key-values

Many of the website registration forms uses a country select element or an autocomplete input field using data from a database.


If this was a single PHP with HTML combined page, there was no problem as you could easily reuse the query made to collect the data. But when using an MVC approach where your controller needs to push the data it fetches from the backend to the form before the form can be passed to the view you're in for a treat. We're addressing the issues for an MVC approach here.

Let's look at our country table first to get an impression how data is being stored in the database.

+-----------+-------------+------+-----+---------+----------------+
| Field     | Type        | Null | Key | Default | Extra          |
+-----------+-------------+------+-----+---------+----------------+
| id        | int(11)     | NO   | PRI | NULL    | auto_increment |
| iso       | char(2)     | NO   |     | NULL    |                |
| name      | varchar(80) | NO   |     | NULL    |                |
| nicename  | varchar(80) | NO   |     | NULL    |                |
| iso3      | char(3)     | YES  |     | NULL    |                |
| numcode   | smallint(6) | YES  |     | NULL    |                |
| phonecode | int(5)      | NO   |     | NULL    |                |
"/>

Truncated by Planet PHP, read more at the original (another 31402 bytes)

Link
SitePoint PHP3 More Joins You Should Be Familiar With (5.2.2016, 17:00 UTC)

There are many ways to JOIN data from two database tables and filter the information you require. Craig Buckler wrote a popular piece on understanding JOINs; namely INNER, LEFT, RIGHT, and FULL OUTER. This article is an extension of that one.

Let’s recap these real quick. Picture two tables, one for customers and one for books to establish a book loan database.

customers table

id firstname lastname book_id
1 Joe Blow 1
2 Jane Doe 2
3 Harry Crow 2
4 Jeffrey Snow 0

books table

id Title
1 Star Wars
2 Jurassic Park
3 Little Women
4 Tom Sawyer

The books table has one row for every book.

The customers table has one row for each customer who can only have one book on loan at a time. If they have no book on loan, the book_id would be 0 or an empty string.

This is a very simple example to make the JOINs as clear as possible to understand!

A LEFT JOIN here would be in the case you want to ask a question such as “show me all customers including any books on loan.

Left Join Venn Diagram

You can see in the image that ALL data in the left circle, or table, is included in the result set. Only data that overlaps from the books table is included from the right table. This means that with a LEFT JOIN, some data in the right table may be excluded.

A RIGHT JOIN would be like asking “show me all the books in my library, along with any customers that have borrowed them.

Continue reading %3 More Joins You Should Be Familiar With%

Link
Thijs FerynMaking the PHPBenelux Conference happen (5.2.2016, 16:41 UTC)

The PHPBenelux Conference edition 2016 took place last week in Antwerpen (Belgium). As an organizer, I’m really happy with the

The post Making the PHPBenelux Conference happen appeared first on Thijs Feryn's blog.

Link
thePHP.ccQuestioning PHPUnit Best Practices (4.2.2016, 22:36 UTC)
Link
SitePoint PHPQuick Tip: How to Permanently Change SQL Mode in MySQL (4.2.2016, 17:00 UTC)

I was working on a legacy project recently and needed to import some data from MySQL 5.5. All the queries in the code worked perfectly in MySQL 5.5, so I assumed an upgrade to 5.7 would be seamless. Not so.

MySQL logo

First I got errors due to DateTime columns being populated with zeros during import, then when running this query:

select * from ebay_order_items where z_shipmentno is null and ExternalTransactionID is not null and orderstatus = 'Completed' and timestamp > '2015-02-25' group by ExternalTransactionID order by timestamp desc

I got this:

Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column '1066export.ebay_order_items.TransactionID' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

Continue reading %Quick Tip: How to Permanently Change SQL Mode in MySQL%

Link
PHP Scripts – Web Development BlogParse html with preg_match and preg_match_all (4.2.2016, 06:40 UTC)
For the most of the web developer which are using the function preg_match, is the function preg_match_all a smaller advantage, but for all others it’s maybe hard to understand. The biggest difference between preg_match_all and the regular preg_match is that all matched values are stored inside a multi-dimensional array to store an unlimited number of matches. […]
Link
PHP: Hypertext PreprocessorPHP 5.5.32 is available (4.2.2016, 00:00 UTC)
The PHP development team announces the immediate availability of PHP 5.5.32. This is a security release. Several security bugs were fixed in this release. All PHP 5.5 users are encouraged to upgrade to this version. For source downloads of PHP 5.5.32 please visit our downloads page, Windows binaries can be found on windows.php.net/download/. The list of changes is recorded in the ChangeLog.
Link
LinksRSS 0.92   RDF 1.
Atom Feed   100% Popoon
PHP5 powered   PEAR
ButtonsPlanet PHP   Planet PHP
Planet PHP