core error

Archive for the ‘php’ Category

Finally Symfony 2.0 was released. This is one of the most popularPHP framworks and now it has been reinvented with this list of new features:

  • It fully follows the PHP standards
  • Extensively uses dependency injection
  • Big improvement in performance
  • Decoupled in 21components all of the separately available in git

For people coming from symfony1 a cookbook has been created showing how the old version differs from the new one.



SimpleXML is a PHP extension (enabled by default after PHP 5.1.2) which provides a simple toolset to convert an XML document into an object so it can be easily used by a PHP application


Before PHP5 handling XML documents was quite painful.  With SimpleXML you just need to create one SimpleXMLElement and from that moment on you can use the XML document as it was a regular object where each node is an attribute containing other attributes/nodes.


$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<title>Back to the Future</title>
<name>Dr. Brown</name>
<actor>Christopher Lloyd</actor>
<rating type="stars">5</rating>

$xml = new SimpleXMLElement($xmlstr);
echo $xml->movie[0]->plot;


cURL is a library of calls included in PHP core which allows to fetch data from remote sites. cURL is much safer than doing include or read_file on remote sites.


cURL is specifically designed for this function so you can do much more things than what you could do with include or read_file. For example, you can add specific HTTP options like timeouts and select th HTTP method (GET or POST).



$buffer = curl_exec($curl_handle);

if (empty($buffer)){
    print "<p>Sorry, is down at the moment.</p>";
    print $buffer;

Why PHP is better?

  • wider community support
  • small learning curve
  • support of a big company (Zend Technologies)

Why Python is better?

  • much easier to develop clean code
  • Django is one of the best web framworks out there

Since performance-wise and feature-wise both languages are quite similar we could summarize this topic in one sentence: if we value the support of a big community over the maintainability of our code we should go for PHP. Otherwise we should pick Python.


__autoload()  is a function which is automatically called in case you are trying to use a class/interface which hasn’t been defined yet.


This is very useful for removing the need of making a long list of includes at the beggining of each script so we can use the classes inside those files.


you can create an autoloader.php file with:

function __autoload($class_name) {
    include str_replace('_','/',$class_name) . '.php';

This file can be included in the main php file (i.e. the index.php of an MVC) so it gets included in all the php scripts in the application. Therefore we could do:

$c1 = new controllers_c1;
$m2 = new models_services_profiles;

As you can see, we can create a model of class profiles without including the file models/services/profiles.php since it is autoloaded by the autoloader php when the class was not found in the first place.


stdClass is php’s generic empty class.


Using PHP’s stdClass feature you can create an object, and assign data to it, without having to formally define a class.


You can quickly create an object out of the stdClass to hold data in it:

$movie          = new stdClass;
$book->name     = "Back to the Future";
$book->director = "Robert Zemeckis";
$book->cast     = array('Michael J. Fox', 'Christopher Lloyd', 'Lea Thompson');


ReflectionClass allows the developer to retrieve information about a class during runtime.


Two of the many uses we can give to this class are:

  1. get a list of the methods before making any call to them (ReflectionClass::getMethods)
  2. make a private method available to be called (quite handy to unitest private methods)


Let’s see an example of the use #2 (testing a private method):

class Foo {
          private function validateData($data) {
		$is_valid = true;
		if($data == 7){
                       return true;
                       return false;

class DataTest extends PHPUnit_Framework_TestCase {
    public function testValidateData()
		$reflection_class = new ReflectionClass("Foo");

		$method = $reflection_class->getMethod("validateData");

		//We need to create an empty object to pass to
		//ReflectionMethod invoke method followed by our
		//test parameters
		$foo = new Foo();

		//Boundary test - False expected
		$this->assertFalse($method->invoke($foo, 5));

		//Good data - True Expected
		$this->assertTrue($method->invoke($foo, 7));


  • None
  • Justin Cooney: Great comic, this made me laugh!
  • Justin Cooney: Interesting, I didn't know about the Google App Engine, I'm glad I found your article on it!