My Way to PHP: Day 6 of 75

Day 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 arrays. The goal is 50 page, that is page 143 which is at the end in the chapter about variables.


However, before I start with the book I want to get done with the pitfalls. I found several sites which cover problems.

My first source is PHP quirks and pitfalls

The first pitfalls is about operator precedence and implicit casting. The operators + - . have the same precedence. That is every statement is evaluated left to right.

And implicit casting happens if you try to add to a string. If PHP can’t convert the string into a number it uses the value 0. The result is this:

echo "foo" . $a + 1 . "bar"; // 1bar

echo "foo" . $a; // foo1
echo "foo" . $a + 1; // 1

The next pitfall which I saw mentioned quite a lot is the old header already send error. It mostly happens because there’s some whitespace before the opening or closing php tag. Because of that the file already sends out content and with that a header.


The next source is PHP Pitfalls.

They offer a nice table of falsey values in PHP. That is values which are considered as false by if().

null      // null
0         // integer
0.0       // float
"0"       // string
""        // empty string
false     // boolean
array()   // empty array

So I read a few more sites on the topic but most behaviors were pretty clear if you knew a bit about the internals. I think that there will be a few more pitfalls which I will find on my way. But for now that’s good enough!


I learned a new term again Microservices. Yesterday, I wrote about the CL tool with a web interface. The idea looks very similar.

In short, the microservice architectural style is an approach to developing a single application as a suite of small services, each running in its own process and communicating with lightweight mechanisms, often an HTTP resource API. These services are built around business capabilities and independently deployable by fully automated deployment machinery. There is a bare minimum of centralized management of these services, which may be written in different programming languages and use different data storage technologies.

In my opinion that’s a good idea – the biggest problem can be speed, though!

We do not claim that the microservice style is novel or innovative, its roots go back at least to the design principles of Unix.

Yep. I’ve written about that, from Day 2:

I think I’ve written about this before but I love the UNIX philosophy of everything is a file. A database is kind of a file but it’s decoupled from the language or tool. That is, you can write your frontend in PHP, your scheduling algorithm in Python, your optimizations in C and it’s no problem because there’s this beautiful thing called a file or database which connects without problem to any of them. Or even better, you don’t have to write much code because you can use existing software!


Fowler talks about the problem of multiple databases. I still think that you should use one database which collects all the data you need. Of course, if one of your application needs a database to – let’s say store the newest data from a web crawler – that’s ok. But it should be a read-only interface. Every change you want to do to that data can only come from the application – not from the ‘end-user’.


One last great point Fowler makes:

Finally, there is the factor of team skill. New techniques tend to be adopted by more skillful teams. But a technique that is more effective for a more skillful team isn’t necessarily going to work for less skillful teams.

I can’t remember who said it but it was a critique on extreme programming. The author of that piece basically said.

Sure XP worked for people like Kent Beck but I think every other method would have worked with people as experienced as them.

It also comes down to what Beck said in XP explained.

Do you play to win or not to lose?


New terminology: Gateway a class which wraps a API so that you can access an object instead of the API directly.


EventSourcing. The basic concept is easy: You want to redo something or be able to look at past events. However, two of the biggest problems are a) external interface and b) internal changes.

While it works quite simple for a Web UI it’s incredible hard for a backend system.

You can see that quite easily. Let’s say that you have some transactions with transaction fees. You calculate these fees when an event happens (a transaction). Over time you can look at the account of an individual because you saved all events.

Now you find that you had an error in your transaction fee method. When you fix it however then all past transactions are different. Are they wrong or not?

Technically they are wrong but they were accepted by the end user. That is you need to retain the old data. However, maybe you haven’t sent out the invoice yet, then they could be wrong!

It’s incredible finicky. There are numerous other examples. For example, you hardcoded the fee and now it changes. You had to rewrite the events including a fee variable which can then be correctly recalculated.


More terminology incoming: Polyglot Persistence. Comes from Polyglot Programming which means use the right language for the right job. Polyglot Persistence means use the right data storage for the right job.


Reporting Database is a database which is used for reporting. It’s read only, you can duplicate data, don’t have to admit to the normal forms. Mostly, doesn’t need up to date all the time. Pretty good idea!


You know what I like about the PHP community? It’s diverse. Most other language communities seem to exist out of people from California. The blog posts / books I read from the PHP community comes from Germany, England, Texas, North Carolina, France, etc.


Here’s a super solid piece of getting the state of a company’s codebase / dev process: Learning a new codebase

TL;DR:

Ask about coding standards, package managements, tests and the deployment process.


I just saw that PHP 5.6 started to support an alternative to func_get_args() with nice syntactic sugar. I complained about that a few days ago and here it is.

function foo(...$args) {
    foreach($args as $arg) {
        echo $arg, "\n";
    }
}

foo(1, 23, 42, 0); 

/*
1
23
42
0 */

Nice!


Ok, back to the book. The chapter about arrays was pretty standard and nothing new – which makes sense because I read about that in the past.

However, in the chapter about variables they mention apc. Let’s take a deeper look into that.

So APC is a opcode cache, that is it saves the opcode / byte code of PHP. However, you can also use apc to store and fetch variables!

You can basically work with apc_store() and apc_fetch().

Example:

$someGiantObject = ...;

apc_store('mygiantobject', $someGiantObject);

// do something

apc_fetch('mygiantobject'); // to read

Since PHP 5.4 – if I understand that correctly – OPcache is the preferred package to apc.

But it only does bytecode caching and doesn’t support memory caching. However, there’s support for memcache and memcached. This is probably confusing, the simplest explanation is that memcache is a daemon based cache and memcached is a library. So for smaller things memchached is probably the easier approach.


Reading about default parameters also brings the static defaults in Python in mind. PHP handles them a bit differently:

function foo($a = array()) {
    $a[] = "1";
    var_dump($a);
    echo "\n";
}

foo();
foo();
foo(); // stays 1

in the case of Python, the output would have been an array("1", "1", "1").


There are sadly no named parameters in PHP. One idea suggested by the book is using arrays:

function foo(array $params) {
    echo "Foo is {$params['foo']} and bar is {$params['bar']}";
}

foo(["foo" => "hello", "bar" => "bye"]);

// Foo is hello and bar is bye

Thanks to the compact array notation it’s quite readable. But your missing type hinting for single parameters which sadly don’t work with this approach quite so simple. Also default values are more complicated, etc.


filter_input() is quite interesting. There’s also filter_var() which works on variables instead of external input. There are several build-in filters.

Here’s a short summary of the most extravagant ones:

Validate filters

  • FILTER_VALIDATE_EMAIL – no more regexs!
  • FILTER_VALIDATE_URL

Sanitize filters

  • FILTER_SANITIZE_EMAIL
  • FILTER_SANITIZE_FULL_SPECIAL_CHARS = htmlspecialchars()
  • FILTER_SANITIZE_URL

Pretty neat!

Example:

$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL); // returns false if not validation

Oh Jesus, the list a recipe to valid CC numbers.

Protip: Never deal with payment if you don’t have to.


Learned something new again. There’s DBA which supports embedded key/value databases aka Berkeley DB style. Several different databases are supported (DBM, NDBM, GDBM, DB2, DB3, DB4, CDB, …). You can store, fetch and delete keys and values and that’s all you need!

Really cool didn’t know that there’s a lightweight key/value DB available.


Most of the other chapters either present ‘special’ functions like graphics or emails – which is something I look up when I need it. Or present stuff I’ve already learned!

Therefore it was rather quick to go through the last few chapters.


Ok, I wanted to reread The Pragmatic Programmer. The last time I read it in 2008 I think and just want to update my knowledge. I remember there were a few gems in there. Let’s see how good the book holds after 6 years.

The second chapter mentions the broken window theory. In case you’re not familiar with the topic. It basically means that the threshold to break a second window is a lot lower than breaking the first.

I’ve seen this theory applied in different contexts and also in projects. Because the authors don’t go into the implementation, here’s how I reached the point of immediately fixing broken windows.

  1. Start small. Don’t try to get everything perfect. You’ll probably either won’t start or stop because it is too much
  2. Make a commitment
  3. Realize that it is hard but don’t back out. It’s like working out. It’s hard, and you may not want to do it, but if you do, you’ll feel great.

The next tip is about change: Show something!

Let’s say you want to introduce a new framework. There’s a lot of discussion. Instead trying to convince people by words do it by action. Build a small product or part of the product you want to build. Show it. Show how easy it was, how much faster it is, how the code quality is higher, etc.


Make Quality a Requirements Issue

Two things. Firstly, present the options: “Do you want it to work in IE5? Sure, we can do that, however it will cost you $30,000 extra? Sure, if it isn’t so important we leave that out.”

Secondly, identify the real problem you need to solve. Sometimes writing code isn’t the solution, sometimes even software isn’t the solution.


DRY is one concept I remember quite well from the book, however I forget the original context. Do you know it?

Here it is:

Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

Knowledge, not code! One example I like is the shows that you can use implicit knowledge. The example takes a class Line which has the attributes: start, end, length. Length is implicit, either take it out or let it set automatically by the setter of start and end.


Interesting observation regarding orthogonality. See how many files / classes / functions you have to change to fix bugs.


Interesting talk about HHVM:

Mostly history about HHVM and a bit of insight about the inner workings. For me, the most interesting insight were:

Facebook already optimized basically everything: Databases, the kernel, networking and then started to optimize PHP

and

I don’t think there are a lot of people who will need the HHVM

and

I don’t know how long they will support PHP in the HHVM.

The background is pretty interesting, starts around 48:00. I wouldn’t bet on it given his argument.


Another good talk. This time about the Proxy Pattern:

It’s good to learn a bit more terminology.

Lazy loading is like lazy evaluation but with the loading of something (often big or computational expensive).

Basic idea is to call this expensive operation only when it’s needed.

A pattern to implement that is the virtual proxy which just wraps around our expensive class.

class Expensive {
    public function __construct() {
        // something expensive
    }
    
    public function getSomething() {
    }
}

class ExpensiveVirtualProxy extends Expensive {
    protected $wrapped;
    public function __construct() {
    }
    
    public function getSomething() {
        $this->init();
        return $this->wrapped->getSomething();
    }
    
    public function init() {
        if(!isset($this->wrapped)) {
            $this->wrapped = new Expensive();
        }
    }
}

Super easy idea. You just wrap the expensive operation and only initialize it if needed.

Then there’s a ghost object which is empty but initializes itself if it’s accessed.

Next is a protection proxy which handles access to the real object. So it’s basically a decorator.

So, smart reference applies more logic than the real one.

Next thing I learned: Fluent interface is an interface with methods which returns the (or sometimes a) object which then can be called again.

$foo = new Foo();
$foo->setA()->setB()->read()->show();

Best Practices for Designing a Pragmatic RESTful API. I like the intro, he talks about two things. First, your API should be good enough to power your UI. Second, most of the writing on the design of RESTful APIs is academic and every writers seems to try to push his own standard – which is pretty annoying.

  1. Start with the resources (nouns)
  2. What actions can you do to them?
  3. Always version your APIs
  4. Filtering, Sorting, Searching as Parameters, e.g. /questions?sort=date
  5. Add fields to select fields in the query parameters
  6. Return the changed resource for PUT, POST and PATCH
  7. gzip your returns
  8. Use JSON for input and output
  9. Use the link header for pagination
  10. Implement rate limiting with its headers (X-Rate-Limit-*)
  11. Authentication should be stateless. Use SSL + token OR OAuth
  12. Use sensible status codes and return an error message

Great article!


Updates Goals (23th October):

  • Learn the most important pitfalls
  • Reread the Pragmatic Programmer
  • Learn about PHPUnit
  • Get an overview of the standard library (SPL)
  • Learn about functional programming in PHP
  • Learn about composer
  • Learn about coding standards
  • Learn about caching
  • Learn a bit more about legacy systems and how to handle them
  • 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.
  • Get a bit more exposure to OOP and OOD

Progress status

Done

  • Learn the most important pitfalls
  • PHP Cookbook by David Sklar, Adam Trachtenberg [done]

In Progress

  • Reread the Pragmatic Programmer [53 of 352 pages]

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]

My Way to PHP: Day 4 of 75

New day! Like every day I edited and published yesterday’s notes. I’m currently on page 105 in PHP Objects, Patterns, and Practice – which is just at the start of using the Reflection API. My goal is 50 pages. That would be to the start of the Singleton pattern.


I also changed the goals a bit and made them clearer.


Back to the book! That’s a pretty cool example using the Reflection API. The author describes how to implement a simple plugin system. Neat!


With OOP think about responsibilities. Don’t try to remodel the world but focus on the problem at hand.


If you use flags or class data with a conditional then it’s a good idea to think about inheritance.


Start with the interface and stay there for a while. Don’t start implementing or thinking about the children too soon.


And we’re back to design patterns. I’m a bit optimistic that the author will present them better as last time.

Problems tend to recur, and as web programmers, we must solve them time and time again. […] Over time, we answer these questions with a greater or lesser degree of elegance and evolve an informal set of techniques that we use and reuse in our projects. These techniques are patterns of design.

Funny, how he describes them less as god-given rules and more as common manifestations.

Patterns are (or should be) essentially bottom-up and not top-down. They are rooted in practice and not theory.

Yeah.


I think I may know why I have a dislike for the hype around patterns. It feels like rote learning. Instead of teaching people HOW to create solutions, the solutions are presented. Learn the problem, learn the solution – don’t learn how you can come up with your own ones.


The examples so far are really good. Also one note: One responsibility in a sense of easy testing and decoupling.

What I mean is that if you have some object handling something which comes out of a database. The object shouldn’t wonder where it comes from.


The GoF recommend that you actively seek varying elements in your classes and assess their suitability for encapsulation in a new type.

I think I now begin to understand why Refactoring and XP become to popular – especially XP. People started using OOP (and patterns) and created flexible monstrosities which nobody understood anymore. And instead of writing valuable code they wrote flexible code. Makes a lot more sense in the context for me.


This book is so much better than the other one. I read the sections on the Singleton and Factory Method. Instead of a short intro and praising the pattern it starts off with a problem and possible solutions. You basically get tricked into patterns. Also the author shows problems with each pattern.


I don’t really have to say a lot about the presentation of each pattern. So far, there wasn’t that much new stuff which surprises me. However, there’s still a chapter about enterprise and database patterns coming up. That could be interesting!

Oh, and I love the examples in this book!


I quite like the Decorator pattern. It’s simple and works. Somehow the biggest problem I see is the speed of execution – but maybe that isn’t really problematic.


The behavioral patterns sound interesting – that is the one I didn’t know before. Let’s see!


This was definitely interesting. After reading through all these chapters which were based on GoF I changed my opinion a bit. I see the usefulness of some of the patterns however I still think that they are overhyped.


After all this exposure I want to see the other side. I mentioned that I read Norvig’s take on it. I also read Paul Graham’s take on it who basically says that either the programming language, an interpreter you write or yourself take care of ‘compiling’. In the case of design patterns it is obviously yourself.


Another criticism comes from M.J. Dominus. What’s interesting is that he talks about Alexander’s book A Pattern Language.

The most important insight from his book is that the pattern language, I quote:

  • does not tell you how to design anything
  • It helps you decide what should be designed

That’s a big difference. Sadly, the author doesn’t provide ideas himself on this. But this also gives me the possibility to let my imagination run wild.

I haven’t read A Pattern Language but just looking at Amazon gives me a good idea. Here’s something from a review:

Alexander and his co-authors present us with over two hundred (roughly 250) “patterns” that they believe must be present in order for an environment to be pleasing, comfortable, or in their words, “alive.” The patterns start at the most general level — the first pattern, “Independent Regions,” describes the ideal political entity, while another of my favorite patterns, “Mosaic of Subcultures,” described the proper distribution of different groups within a city. The patterns gradually become more specific — you’ll read arguments about how universities should relate to the community, the proper placement of parks, the role of cafes in a city’s life.

What immediately popped into my head were things like DRY (Don’t repeat yourself). Good names. Hiding complexity. Making the code easy to understand.

It basically boils down to defining the equivalent of pleasing, comfortable – “alive” software. There are multiple groups: users, coders, management, etc. If you really think about it, it comes down to needs and then trying to match them.

Somehow the phrase ‘Write for your audience’ came into my mind. I’ve never seen a book about programming mentioning it but I think it applies.


I also read some criticism which basically says that GoF is just applied SOLID. I knew the phrase but never looked into it.

S = Single responsibility principle. We talked about that

O = Open/closed principle: The basic idea is that a software entity is open for extension but closed for modification. Betrand Meyer even gone so far that when a class is completed there will be no new methods or functionality – only bug fixes.

L = Liskov substitution principle: I read about this. It basically says that if an object of type A then you can use any child of A instead of A.

I = Interface segregation principle: Pretty simple. An implementation shouldn’t depend on interface it doesn’t use.

D = Dependency inversion principle: Abstract so that you don’t have to care about low-level. Example: You want to copy some characters. Instead of just copying them, you first translate those characters to a higher-level entity, copy this, and translate them back.


Cool, the next chapter is about enterprise patterns. It’s based on the book I wanted to read let’s see how good the chapter is and then maybe I’m change my goal based on that.


Somehow I think that I should read the GoF myself. I experienced it before that source material gets changed over time and loses some of the details which are really important. Maybe, maybe.


The first pattern is called registry and is basically a Singleton you can set and get. Seeing how this registry is used for multiple variables all I can see it that it’s reimplements the variable management of PHP but without the GC.


I skimmed most the tons of code but I got a good idea of the patterns presented in this chapter. It’s definitely interesting, however, I don’t think I need to read the book right now. It’s more on the platform than the application side.

After finishing this book I my next few books won’t be on the topic relating to OOP or Design Patterns. I reactivated quite a lot these last few days of the stuff I learned in the past and I know that I need to practice it a bit to get back on track.


Getting down to some serious architecture is a more rewarding prospect than writing some glue to stitch together three or four existing components.

I don’t necessary agree. If I can solve the same problem using 3 existing components and some other guy writes everything from scratch then I’d say reusing is more rewarding. But I wrote about this a lot and I think I’m part of the minority of people who like to solve problems instead of writing code.


So there is PEAR (which I recognized from the past). PEAR2 which has the Pyrus installer and composer – which seems to be the new kid on the block and a bit like leninigen or pip.

PEAR seems to be some what deprecated or on its way to there.


The author recommends phpDocumentor which uses the JavaDoc comments to create a documentation. It also seems to be still updated.

From the screenshots in the book the functions looks pretty good. Even without writing any documentation you will get an overview over your classes, type hinting, etc.

This can be quite valuable if you’re new to a project and want to get an idea of its architecture.


The DocBlock is like in Java or C++ or whatever. Most important tags are:

@var for fields

@param and @return for functions

Also nice to have:

@uses which links to something and this links back

@example


Updated Goals (October 21st):

  • Learn more about the PHP OOP and Design Patterns
  • Learn the most important pitfalls
  • Learn the right and idiomatic way to do PHP development today
  • 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.

Progress status

Done

In Progress

  • PHP Objects, Patterns, and Practice by Matt Zandstra [378 of 532 pages]

My Way to PHP: Day 3 of 75

And again I woke up early! I just spend about half an hour editing todays post (that is yesterday when you read this). Now I’m looking for more resources especially about PHP in detail. I added one yesterday which I will probably read after the current one. I also added the top stackoverflow questions on PHP which I will selectivly read – probably the top 20-30 pages.

In Learning PHP Design Patterns I’m currently on page 41. My goal is 50 pages, that is page 91. That will be around the fifth chapter which is about the factory method design pattern.


In the meantime I’ve searched for some more interesting resources which support my goals.

While looking for books I found this video about legacy code in PHP:

The speaker also wrote a book which I will probably read in the future.

  1. Most PHP software starts out as one script file
  2. Afterwards it gets put into different files but it won’t be designed in a modular way

Jesus, the code he shows in the video.

Incremental goals

  • Keep the application running
  • Consolidate classes for autoloading
  • Convert globals to injected dependencies

Interesting talk! I learned about the autoloading ability of PHP and know what dependency injection means. And I finally understood why there’s code like:

$foo = new Foo(new Bar());

 


Now back to Learning PHP Design Patterns.

First principle: program to an interface, not an implementation.

Ok, the idea is that you use interface as your main type while writing code. Let’s say you have an interface called DatabaseInterface. You write implementation for it, e.g. one for MySQL, one for SQLite, etc.

Now, if you want to use a database as a parameter you don’t use the SQLite type but rather DatabaseInterface. That’s allows you to switch out implementations. And most important you can write mock implementations for testing.


Here’s also something new. Each object is instance of its class (obviously) but also instance of the interface the class implements:

interface I {
    public function foo();
}

class A implements I {
    public function foo() {
        echo "I'm A";
    }
}


$a = new A;

var_dump($a instanceof A); // true
var_dump($a instanceof I); // true

Second principle: favor object composition over class inheritance.


One thing to consider in choosing a design pattern is what will vary in a design. […] what you want to be able to change without redesign.


Factory Method Design Pattern

Ok, the idea about creational design patterns is that the object initiating a new object shouldn’t need to know how to create the object.

Which is also basically the idea behind the factory method DP. Just with multiple subclasses which implemented one interface.


While thinking about the usage of the factory DP I looked around and found one blogpost about GoF. The author talks a bit about the misunderstandings of Design Patterns which helped me understand the usage of them.

The main misunderstandings are:

  • The authors of the book thought they invented the patterns
  • The book says that you have to use the patterns from the book to write good software.
  • The book says that the patterns in the book are the only and most important design patterns

All this is totally, absolutely, completely, outright and utterly wrong. The intent of the book was totally different than many people seem to think today.

This was pretty useful! I actually wondered why something like the Factory became so popular and I can see the usage of it in SOME cases. And that’s it. It isn’t the right way to do it generally but in the cases you need to solve that problem.


Now all this hype around GoF and OOP makes a lot more sense…

Ok, back to the book. Yeah. So I’m through the chapter about Factory Method DF. The most important things I learned – so far – were a) dependency injection (from the video) b) the first principle of DF and c) the idea that you might want to generalize the object creation.


Prototype Design Pattern Ok, the idea is that you create a prototype object which then can be cloned.

Here’s what I would do without looking at the pattern:

  • Either just write a function which creates the object, does all the defaulting and returns the object
  • Or inherit the object and do the same thing

I would probably do the first thing.

Now, I looked at the pattern and it uses inheritance. Ok, I forgot to code against the interface. But otherwise pretty good.


Structural Design Patterns: The idea is to combine stuff to make up new stuff.

Adapter Pattern: The idea is that you want to use A and B together but they don’t fit. Therefore you intercept an adapter.

I’m seriously wondering why GoF became so popular.


Decorator Design Pattern: Add stuff to an existing object without changing it.


Behavioral Design Patterns: How do objects work together / communicate?

Template Method Pattern: When you want apply different functions but the functions vary.

You might know what I was thinking.

function foo($a, $b) {
    if(call_user_func($a) > 10) {
        echo call_user_func($b, 25);
    }
}

foo(function() {return 100;}, function($x) { return $x * $x; });

I just leave this uncommented.


State Design Pattern: Change behavior when state changes


Okay, dokey. The next part is called MySQL and PHP Design Patterns

Proxy Pattern: Stay in place for the real deal.


Strategy Pattern: Use different algorithms


Chain of Responsibility Pattern: Put something through a lot of things.

The basic idea is that you make a linked list with processing in it.


Observer pattern: Hey look. Something was updated.

Actually php offers SPL functions for that: SplObserver, SplSubject and SplObjectStorage. Otherwise you had to poll an object all day or attach something to the setters.


Freaking amazon reviewers were right again. The first 4 chapters (basic OOP) were alright. The following chapters not so much. I skimmed most of the later chapters but I wasn’t impressed (which led to the skimming btw).


So, I have another book lying around which also has patterns in its name. But the ToC looks great. But after reading through the last book I want to read some Stackoverflow question on PHP!

When to use self vs $this?

I didn’t even know self existed. self refers to the class in which it was mentioned, that is not at run-time. If you want to use a static method use static::foo instead of self::foo. Why? Because if you use self and you inherit the self will still relate to the parent class.


How should I ethically approach user password storage for later plaintext retrieve?

What I found most interesting was a link to Diceware. If you have used Electrum you may have seen Diceware in action. The idea is that you present a password as a list of words. The name Diceware comes from the idea that you roll the dice X times and the resulting number is mapped to a word.

You don’t have to roll the dice yourself of course. What’s pretty neat is that you have a pretty high entropy with just 6 words. And it’s easier to remember than some random string: major car light balloon paper radiator.


How to check if a string contains a specific word

The beauty of strpos or better of fucking implicit casting:

var_dump(strpos("a string", "a")); // 0 (okay, it is at position 0)
var_dump(strpos("a string", "string")); // 2
var_dump(strpos("a string", "banana")); // false
var_dump(strpos("a string", "a") == false); // true

How foreach actually works?

The question is actually really interesting, read it yourself. The answer is insanely detailed and interesting and explains a lot. Basically it has to do with the GC and reference count. That is behavior for foreach differs between using a variable or outputting the array directly.


After 7 pages of questions I go back. But nonetheless I tried out the use keyword which allows closures.

function foo($a) {
    return function ($b) use ($a) {
        return $a * $b;
    };
}


$multi_two = foo(2);
$multi_five = foo(5);

echo $multi_two(10); // 20
echo $multi_five(20); // 100

Works pretty nice! :D (I also just noticed that I don’t have to use call_user_func.


Great. Now that I’m far enough away from the last book I’m going to start the next one: PHP Objects, Patterns, and Practice. The ToC sounds beautiful. Let’s see if the content is as good.


Also one thing in case you’re wondering how I can plough through so much stuff. I have all day for that. I think the last two days I spend about 8 – 10 hours learning or so.


I’ve just read that there are generators in PHP and checked it out.

function incx() {
    $i = 0;
    while(1) {
        yield $i++;
    }
    
}

foreach (incx() as $i) {
    if($i > 20) {
        break;
    }
    echo $i;
}

But you can’t seem to use the iterator functions to use it:

function incx() {
    $i = 0;
    while(1) {
        yield $i++;
    }
    
}

$gen = incx();

var_dump($gen); // object#1 <0> { }
var_dump(current($gen)); // false
var_dump(next($gen)); // false
var_dump(each($gen)); // false

The third chapters – which talks about type hinting – brings up an interesting problem. What if you have a function which takes a boolean.

function foo($answer) {
    if($answer) {
        ...
    }
}

Now, because bool is a primitive you can’t use type hints. What should you do? He shows various approaches, from trying to fix it, to using comments. But the last example – and the one I think he also prefers – is using the is_ functions. He uses die() to exit the function. This would be my approach:

function foo($answer) {
    if(!is_bool($answer)) {
        throw new InvalidArgumentException("foo expects a boolean. Input was $answer");
    }
    if ($answer === true) {
        ...
    }
}

I wonder if that exception is exploitable – I sincerely hope that the input get escaped if it gets outputted to the page.

I just tested compileonline.com which doesn’t escape the expection. However, writecodeonline.com which I mainly use – so far – does escape it.


What I really like about the author (and book) is that he writes clean code in a sense of using libraries when adequate. E.g. he shows abstract classes and implementing one for an XML Writer. And he actually uses XMLWriter instead of writing one himself. Really nice!


I just pondered when I stopped writing PHP. I think it was around 2007 and my hosting provider still used PHP 4.X. It also was probably around September or October. 7 years ago – oh man. But the language developed a ton since then and I really like a lot of the changes!


I’m still thinking about traits. It’s nice that you can use its functions but on the other hand you could also just use a static method or initiate an object.

Also quite interesting, traits have full access to the class using it.

trait A {
    public function foo() {
        $this->a = 40;
        echo "a $this->a, b $this->b, c $this->c";
    }
}

class B {
    use A;
    private $a = 23;
    protected $b = 22;
    public $c = 21;
}

$b = new B;
$b->foo(); // a 40, b 22, c 21

Edit: Funny, later he wrote about exactly about that.


There’s a lengthy section about exceptions which is great. Most books neglect that!


There are quite some functions which allow to dynamically react to undefined method calls, setters, getters, etc. The author calls it interception. More at php.net

These methods are also called magic methods and like magic they introduce uncertainty.


The scope of __clone() is the new copy of the object. Also it’s a shallow copy. That is, you have to explicitly add a clone of all object variables in your class to __clone().


I have to say that so far this book is excellent!


Interesting method to handle file includes. Add your directory to the include_path. You can even do that with .htaccess:

php_value include_path value .:/path/to/lib/php-libraries


Autoloading (like described in the video above). You can use a simple autoloading which comes with PHP. It will search for a class name in lowercases as a file. And if you namespaces in the directory.

spl_autoload_register();

$foo = new Foo(); // will search foo.php or foo.inc
$bar = new baz\Foo(); // will search in baz/foo.php (or inc)

I’m done for today. But I thought about that after finishing this book I will expand my goal a bit. I was way faster that I initially estimated. And think it’s a nice idea to make the goal a bit harder.

Goals:

  • Learn more about the PHP OOP and Design Patterns
  • Learn the most important pitfalls
  • Learn the right and idiomatic way to do PHP development today
  • Read Patterns of Enterprise Application Architecture
  • Learn Symfony2
  • Learn the intricacies: how does the interpreter work, the OOP system, etc.
  • Get a overview over the standard library
  • Learn about functional programming in PHP
  • 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)

Progress status

Done

  • Learning PHP Design Patterns by William Sanders

In Progress

  • PHP Objects, Patterns, and Practice by Matt Zandstra [105 of 532 pages]