Posts

Advanced PHP: 7 useful techniques

In human languages, not all words are equally used, but they all have a purpose and unique power. The same is true for programming languages. A big part of the code we write is just variables, functions, classes, and loops. They are part of the basics you need to know to get your application done. Today I am not going to talk about them. Instead, I'll show you some of the PHP techniques I rarely use but have great impact.
Let's get started. Anonymous functions and classes Sometimes in your code, you must provide a callback function (in usort for example). Instead of defining a global function, you can create one dynamically, use it once or twice and remove it (if you want). It can also save you some time thinking about the right name (because naming things is important). In the following example an anonymous function is used as a callback to help sort people by age. $list = [ ['name' => 'John', 'age' => 31], ['name' => 'Mike…

Using MySQL Stored Procedures with PHP mysql/mysqli/pdo

Using MySQL Stored Procedures with PHP mysql/mysqli/pdoWondering how to use stored procedures with PHP and MySQL? So was I and here’s what I’ve learned. In this tutorial I’ll explain how to use PHP (I’m using 5.2.6) to call MySQL (I’m using 5.0.2) stored procedures using the following database extensions: MySQL – http://php.net/manual/en/book.mysql.phpMySQLi – http://php.net/manual/en/class.mysqli.phpPDO – http://php.net/manual/en/class.pdo.phpFirst we need to setup our enviroment which consists of a new database with one table and two stored procedures. In your db tool of choice (I’ll be using the MySQL Query Browser) create a new database named test. After you create the new database, make sure to add a user called example with password example to the database and give it read access. CREATE DATABASE `test`; Now create the table users: DROP TABLE IF EXISTS `test`.`users`;
CREATE TABLE  `test`.`users` (
`users_id` INT(10)UNSIGNEDNOT NULLAUTO_INCREMENT,
`first_name` VARCHAR(100)NOT NULL,
`…

TDD of a Simple App in PHP

TEST DRIVEN DEVELOPMENT in PHP In this tutorial, I will present an end-to-end example of a simple application - made strictly with TDD in PHP. I will walk you through each step, one at a time, while explaining the decisions I made in order to get the task done. The example closely follows the rules of TDD: write tests, write code, refactor. Step 1 - Introduction to TDD & PHPUnit Test Driven Development (TDD) TDD is a "test-first" technique to develop and design software. It is almost always used in agile teams, being one of the core tools of agile software development. TDD was first defined and introduced to the professional community by Kent Beck in 2002. Since then, it has become an accepted - and recommended - technique in everyday programming. TDD has three core rules: You are not allowed to write any production code, if there is not a failing test to warrant it.You are not allowed to write more of a unit test than is strictly necessary to make it fail. Not compiling …