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.

Advertisements


What?

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

Why?

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.

How?

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

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


What?

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.

Why?

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).

How?

<?php

$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,'http://example.com');
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);

if (empty($buffer)){
    print "<p>Sorry, example.com is down at the moment.</p>";
}else{
    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.

What

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

Why

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.

How

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.

What

stdClass is php’s generic empty class.

Why

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

How

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');

What

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

Why

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)

How

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;
                }else{
                       return false;
                }
          }
}

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

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

		//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));
    }
}




    • 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!

    Categories