in Back to PHP

My Way to PHP: Day 5 of 75

I’m currently on page 378 in PHP Objects, Patterns, and Practice. Which covers branching with git. My goal is again just 50 pages which would be the chapter about git, PHPUnit and Phing. I also just published yesterday’s post.


So, branching. Interesting topic I’m going to reread some of the stuff about this topic after I read the author’s point of view on it.

His strategy is pretty simple. The master (trunk) is the most up-to-date version. For each version there’s a branch. The branch only receives bug fixes which are then merged into the trunk. And that’s it!


A bit elaborate version is presented by Vincent Driessen. He introduces additional branches for versions and feature branches.

A bit more simplified model is by presented by Drew Fradette. There are only three branches. Besides the master branch, there’s a develop branch for development, a release branch for staging and the master branch with tags for version numbers. The master is identical to production. So a bit different to the other two.

This model is similar to what I would do instinctively. I think master should be production, develop is obviously a branch and there’s a staging branch.

The last model I found is by Juan Batiz-Benet. This one is really simple. The master is always up to date. You branch for changes and rebase your branch and merge back into master without fast-forward.


Test exceptions.

Mock object support in PHPUnit is also pretty cool!


The chapter about CI is neat. The author presents a tool chain existing out of:

  • Git for VC
  • Phing as a building tool
  • PHPUnit for Unit tests and code coverage
  • phpDocumentator for documenting
  • PHP CodeSniffer for coding standards

For one of my biggest projects I build a similar tool chain. It’s been awhile since then but the chain had:

  • SVN
  • make
  • No testing suite instead good old asserts
  • Doxygen
  • Lint

The last tool is also something I would add to the existing tool chain for PHP. PHP Mess Detector seems to do a good job.

Nonetheless I may take a while to set that up but if it runs it’s just beautiful.


The author uses Jenkins which seems pretty much standard today for CI tools.

While looking for Jenkins support for PHPMD I found this nifty website called: Template for Jenkins Jobs for PHP. It offers a whole tool chain with configured Ant files (instead of Phing). And here’s some example.


The last chapter was great. In this the author summarized every important points he made. If you have absolutely no time, just read this chapter. It’s just 8 pages long.

He also mentioned a few things he didn’t cover like Wikis and Bugtrackers.


A great book! I’d say that it covers quite a lot and especially important topics. It’s great if you programmed for some time or you want to up your level of professionalism. Splendid!


Now, I want to relax a bit and continue reading Stackoverflow questions! Yeah!

List of Big-O for PHP functions

super interesting post! array_key_exists and isset($array[$idx]) are faster than in_array or array_search.

array_push and array_pop are O(1) but array_shift is O(n).


Can I try … catch a warning?

Also interesting. There’s a function called set_error_handler() which allows you to create individual error handlers. So you can basically write one which throws Exceptions instead of errors.


Two-way encryption: I need to store passwords that can be retrieved

Great question! I wondered that myself a few months ago. No worries, I didn’t build a product using passwords since then.

When I thought about it I didn’t have a good answer. The problem was that you needed a key to decrypt the passwords. However, even if that key is only in the RAM it’s still accessible if the server gets compromised.

Let’s see what the answers have to say. Not much better. If there’s a user activating the process – sure you can use his additional key. But my product didn’t use that. Instead it needed the password every 10 minutes or so in a cronjob.


Next I want to work through the PHP Cookbook. It’s quite long but I probably know some of the recipes and internals already which speeds up the reading.


The Preface is actually useful and provides a link to Planet PHP which aggregates blog posts about PHP. I forgot about the whole planet blog thing but that’s great! I will sprinkle in some articles if there are insightful and relevant in the future.


Utilize dynamic Dispatch interesting blog post on dynamic dispatching in PHP and traits. Not that much new stuff but nice to know a new term :D.


Installing Composer Packages – Version no. One of my goals is to learn more about Composer. One nice feature it offers is that you can use composer require vendor/package to automatically add a package into your composer.json. The other one is the tilde operator which translates the version requirement into >= version no. and < major version no.. For example: ~0.5 expands into >= 0.5, <1.0.


Back to the book! Besides heredoc strings there are also nowdoc strings which don’t interpolate variables. Here are examples of both:

$heredoc = <<<END
  This is some text.
  Thank you!
END;
        
$nowdoc = <<<'END'
  Will this be interpolated:
  $heredoc
  Nope!
END;

Streams are pretty great. I read about them a bit: Understanding Streams in PHP – they are basically like streams in other languages like Java, C++ or Python.

Here’s an example of using fputcsv to output to output instead of a file:

$data = array(
    array(1, "Peter", "male"),
    array(2, "Mary", "female"),
    array(3, "Mike", "male"));

$fh = fopen("php://output", "w");
foreach($data as $line) {
    fputcsv($fh, $line);
}

/*
1,Peter,male
2,Mary,female
3,Mike,male
*/

 

You could also either write a stream wrapper to send the output directly into a variable or use the output buffer ob_start()and ob_get_contents() to save it.


There’s a recipe about comparing floats. Floats are generally pretty complicated thanks to their representation. If you want precise calculation (e.g. for money) don’t use floats. Instead use something that can handle a decimal representation like BCMath.


Random number generators. Also a tricky topic. Generally, don’t use the standard random functions if you work with cryptography. In the PHP docs someone compared the rand() function with mt_rand(). Both are not suitable for crypto stuff – if you need that check out openssl_random_pseudo_bytes().

Here’s a picture of rand() on the left and mt_rand() on the right:

rand-vs-mt-rand


Also interesting. You can use the associative array notation with the yield statement.

function foo() {
    $i = 12;
    $j = "a";
    yield $j => $i;
}


foreach(foo() as $key => $value) {
    echo $key; // a
    echo $value; // 12
}

 


I’ve written about time before, also a tricky subject. Thankfully, there’s DateTime and DateTimeZone. Here’s an example:

$now = new DateTime("now", new DateTimeZone("Europe/Berlin"));
echo $now->format("H:i:s"); // 21:47:46

$now = new DateTime("now", new DateTimeZone("UTC"));
echo $now->format("H:i:s"); // 19:47:46

 

Generally, it’s a good idea to save your dates in UTC and save the user’s Timezone.


The modify method of DateTime is pretty nifty:

$now = new DateTime("now", new DateTimeZone("UTC"));

$now->modify("next Sunday");

echo $now->format("d.m"); // 26.10

 

Some other strings that work are tomorrow, sunday next week, last sunday, etc.


Really solid approach to the layering of web applications is presented by the Action-Domain-Responder. You may know MVC. When I read first about MVC and successively (especially in an old book about Smalltalk) I learned that web frameworks don’t really implement it as presented about 15 – 20 years ago. I also felt that it wasn’t just right.

In contrast, ADR actually makes sense and is implementable. It’s consistent – especially in my experience. The basic idea is pretty simple. You got a model which handles the data, internal logic, etc. An action which is the bridge between domain and response. And the response which takes care of the UI. Simple but great.

My ideal was that you can take any command line tool and plug it into some thing so it works with the web. It isn’t there yet but it’s pretty close.

I also find the MOVE model interesting.


Another interesting insight was that the view in MVC isn’t just the template but also the response code which is mos often handled by the controller.


In ADR and  the “Domain Payload” Pattern the ADR is beautifully illustrated. The general idea is that the Domain sends all the information the response will need – just like my ideal of the CMD with interface. This value from the Domain includes different return states or return classes which then are handled by the responder quite elegantly.

It may take a while to read up on the model and through the code but it’s great and worthwhile!


I feel so great being back. It’s hard to describe but it feels like I’m home again.


Updates Goals (22th October):

  • Learn more about the PHP OOP and Design Patterns
  • Learn the most important pitfalls
  • Learn about PHPUnit
  • Learn about automated deployment / CI
  • Learn about composer
  • Learn about coding standards
  • Learn about caching
  • Learn a bit more about legacy systems and how to handle them
  • Get an overview of the standard library (SPL)
  • Learn about functional programming in PHP
  • Learn Symfony2
  • Learn a bit more about MySQL
  • Write at least one web app using Symfony2 and its core components (templating, testing, forms, validation, security, caching, i18n)
  • Learn the intricacies: how does the interpreter work, the OOP system, etc.
  • Reread the Pragmatic Programmer
  • Get a bit more exposure to OOP and OOD

Progress status

Done

  • PHP Objects, Patterns, and Practice by Matt Zandstra [done]
  • Get the basics of OOP and Design Pattern
  • Learn about automated deployment / CI

In Progress

  • PHP Cookbook by David Sklar, Adam Trachtenberg [93 of 820 pages]

Write a Comment

Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Webmentions

  • My Way to PHP: Day 6 of 75 – panictank

    […] 6! Still a bit hyped :). I just published yesterday’s article. In the PHP Cookbook I’m on page 93 which is just at the beginning of the chapter about […]