Home
About
Search
🌐
English Română
  • CodeIgniter 1.7 Packt Publishing – Book review part 2

    Citește postarea în română

    Mar 22, 2010 CodeIgniter PHP4 php5
    Share on:

    1847199488

    CodeIgniter is an open-source PHP framework build for RAD.
    The book CodeIgniter 1.7 from PacktPublishing, written by  Jose Argudo Blanco and David Upton is trying to build an overall image of of the CodeIgniter framework,  as an addintion to the user guide.

    The book is not a reference, and that becomes obvious with every chapter, each time a new feature of the CodeIgniter framework is introduced the user is directed to the corresponding page of the module in the user guide or wiki. Even more, suggestions are made for alternative modules that can achieve a similar task in a different way.

    Authors say that only minimal knowledge of PHP is necessary to read this book, a promise that is hard to keep in my opinion, usually books that target novice to medium skill readers are saying something like that. To my surprise they ware right, the reader only needs PHP4 knowledge. And when it comes to OOP not even PHP5 features are required, just the PHP4 object model. In this book even the copping objects by reference is described! Of course copping objects by reference is no longer a relevant issue with PHP5, but taking this in to consideration that is an PHP4 framework it was a good idea to mention and explain it. It seems like there are still PHP4 server on the Internet… that is just sad…

    Reading the book flashbacks came to my mind with pieces of code written directly in PHP and the hell of debugging them, trying to understand other peoples code… what times… horribly of course. The reasoning behind this concepts of re usability in a framework is just  great: “Possibly you like typing regex. Some people like lying on a bed of nails…”. When a developer, especially a beginner, is reading something like this it makes him understand that you don’t have to reinvent the wheel each time, but just use solutions already developed by others.

    Comparing between different available solutions I believe is the most funny part of a book like this. Is difficult or even impossible to compare for instance Zend Framework or even CakePHP with CodeIgniter. After all almost all frameworks say the same thing, just download and start working. Personally when I need to use a module from Zend Framework I just load the autoloader and get to work. The comparison shows CodeIgniter as a winner for most users, as expected, the reasoning behind that claim is pretty honest.  Afterall is a small framework and does not have complex features like autogenerating CRUD. I remember a Java book where the author was representing the fact that C++ is faster then Java as a disadvantage, of course was just silly.

    I sometimes had the impression that the terms were wrongly defined. The authors are using “small” mistakes in terms to explain what’s actually happening in the background. When you are working with MVC frameworks some of the notions are simple and obvious, but for a developer that’s not familiar with that terms there are quite hard to comprehend.

    And to continue with the mistakes in the book, I’ve found a few. Quite a difficult moment when you start learning something new. Fortunately there are quite obvious because they result in errors, and if you read chapter after chapter you’ll know what the issue is and how to fix it.

    Examples are pretty consistent and well documented. When a new concept or module is introduced it is fully explained in detail.

    The resulting apps are not very complex, for instance at the end of the book there isn’t a full complex app like a CMS, rather modules and the way there are combined is explained. The user will have to decide in the end how will his app look and feel. For instance in chapter 13 pagination and ordering is explained. When pagination is used everything is OK, but when ordering is introduced the pagination begin to slip. It took me about 5-10 minutes to fix the issue, but it would have been nice for the authors to fix the issue themselves.

    Overall is a good book, especially for users that have no prior knowledge of CodeIgniter, is just like is presented by the authors, a book for developers that want more productivity in there work or just what to see what other tools are out there.  The book does not present full solutions like a CMS or a shopping cart, but rather what this framework has to offer.

    An advanced developer can understand from this book the structure of the CodeIgniter framework, possibly to compare it to other popular frameworks, without loosing time with complex and irrelevant examples.

  • Google now knows where I live

    Citește postarea în română

    Mar 1, 2010 google maps yahoo maps
    Share on:

    More then an year ago I wrote a blog, where I was telling that Google doesn’t know where I live.

    The issue was that Goole Maps had a very poor coverage for Romania, and Yahoo! Maps is a way better alternative for this part of Europe. I still believe that Yahoo! Maps is a better alternative but I was surprised by one thing. Occasionally I’ve visited Google Maps to see how things are going with the coverage of Bucharest, basically it was represented a single way to cross the city from highway A1 to A2, and nothing else.

    About a month ago, I went back to check what was new because I needed to build an Romanian tourism app and… surprise, surprise, even though the satellite photos are of lower quality now, I could find my street using the search and even the block where I live. Even more I was able to calculate alternative routes to various  locations in Bucharest.

    It looks like Google has began to have a little more interest in this part of central Europe, now allowing for a real competition with Yahoo!. This is a great advantage for Android phone users who use Google Maps for there GPS app. Few years ago, to user Google Maps in Romania it was completely useless, you could see the satellite images, but that was about all, you could not find useful addresses, and if there ware marked on the map you could not automatically find a route to them.

    Eventually I used Google Maps for my app, even though I still believe that Yahoo! Maps is a good alternative, as far as quality and API. Speaking of API, both of them seem ok and I don’t king that’s a criteria to difference them between the two giants.

    A thing a little shocking about Google knowing where I live it was on an evening when curios to see if my GPS works on my mobile phone I went on my balcony. Everything was ok, it found me within 50 and 100m on the Google map, using my wireless connection for the data transfer. Another evening a friend came over to tried the same thing, using the same connection to the Internet, same Google Maps app, and he was found within 100m.

    Nothing unusual in theory, but everything changed  the next day when he told me that his phone didn’t have GPS, and what we ware seeing was the date from the last time I connected my device… so Google now knows where I live… literary…

  • Oracle to buy Sun – a new chapter

    Citește postarea în română

    Jan 26, 2010 MySQL Oracle Sun
    Share on:

    It seems like Oracle received the unconditional approval from EU to by Sun according to Yahoo! News.

    About and year and a half ago I was considering the MySQL Certified Developer exam. Now I’ve become Sun MySQL Certified Developer and it seems like I’ll even become Oracle MySQL Certified Developer. And all of this with no extra charge! 🙂

    The good part is that we’ll become Oracle developers even if we don’t want or plan to (ok, is not exactly Oracle, but one of there products).

  • Factory method pattern using PHP 5.3 and late static bindings

    Citește postarea în română

    Jan 24, 2010 Design patterns PHP php5 php5.3
    Share on:

    Factory method design pattern introduced by GoF (Gang of Four) which I’ve talked about in a previous blog, has the base idea of a method that generates objects. Most implementations are “poor”, because they use a lot of hard-coding for the factory (just like me in the previous blog).

    PHP 5.3 offers the possibility of a very interesting implementation using “Late static bindings“.

    The classes from which the objects will be generated are:

     1// abstract base class that will be inherited
     2abstract class Drink {
     3
     4    // ingredients
     5    protected $ingredients;
     6
     7    // public method for producing the drink
     8    abstract public function MakeDrink();
     9}
    10
    11// a child class for tea
    12class Tea_Drink extends Drink {
    13
    14    // ingredients for tea
    15    protected $ingredients = array('tea', 'sugar', 'mink', 'water');
    16
    17    // make tea
    18    public function MakeDrink() {
    19
    20        // make tea
    21    }
    22}
    23
    24// another class for Bloody Mary
    25class BloodyMary_Drink extends Drink {
    26
    27    // ingredients for Bloody Mary
    28    protected $ingredients = array('votka', 'salt', 'tomato juice');
    29
    30    // make Bloody Mary
    31    public function MakeDrink() {
    32
    33        // make BloodyMary
    34
    35    }
    36}
    

    The idea is to have an abstract factory class to extend as simple as possible when creating each new factory class.

    PHP 5

    In PHP 5 the class will look something like this:

     1// abstract Factory class
     2abstract class absFactory {
     3
     4    // name of the base class
     5    static protected $base_class = '';
     6
     7    // factory method
     8    public static function getInstance($type) {
     9
    10        // name of the resulting class
    11        $class_name = $type . '_' . self::$base_class;
    12
    13        // check if class exists
    14        // here you can add an autoloader
    15        if(!class_exists($class_name)) {
    16            throw new Exception( 'Class ' . $class_name . ' not loaded!');
    17        }
    18
    19        // check to see if the class inherits the base class
    20        if(!is_subclass_of($class_name, self::$base_class)) {
    21            throw new Exception(
    22                'Class ' . $class_name . ' is not a child of ' . self::$base_class
    23            );
    24        }
    25
    26        // new object
    27        return new $class_name;
    28
    29    }
    30
    31}
    

    Because the getInstance() method is static the property will be static too.

    If we try:

     1class DrinkFactory extends absFactory {
     2
     3    static protected $base_class = 'Drink';
     4}
     5
     6try {
     7
     8    $obj = DrinkFactory::getInstance('Tea');
     9
    10} catch (Exception $e) {
    11
    12    echo $e->getMessage();
    13}
    

    The output will be:

    1Class Tea_ not loaded!
    

    Because of the “self”, we can’t just call the method using the child class because the value of $base_class will be “” and not “Drink”, we must overwrite the getInstance() method. Which is quite “complicated”.

    A working version in PHP 5 will be:

     1class DrinkFactory extends absFactory {
     2
     3    public static function getInstance($type) {
     4
     5        self::$base_class = 'Drink';
     6
     7        // factory method of the base factory class
     8        parent::getInstance($type);
     9
    10    }
    11
    12}
    13
    14try {
    15
    16    $obj = DrinkFactory::getInstance('Tea');
    17
    18} catch (Exception $e) {
    19
    20    echo $e->getMessage();
    21}
    

    But is not exactly “elegant”.

    PHP 5.3

    Here we have “Late static bindings”, which is basically introducing the work “static”.

    The base factory class will look something like this:

     1// abstract Factory class
     2abstract class absFactory {
     3
     4    // name of the base class
     5    static protected $base_class = '';
     6
     7    // factory method
     8    public static function getInstance($type) {
     9
    10        // name of the resulting class
    11        $class_name = $type . '_' . static::$base_class;
    12
    13        // check if class exists
    14        // here you can add an autoloader
    15        if(!class_exists($class_name)) {
    16            throw new Exception( 'Class ' . $class_name . ' not loaded!');
    17        }
    18
    19        // check to see if the class inherits the base class
    20        if(!is_subclass_of($class_name, static::$base_class)) {
    21            throw new Exception(
    22                'Class ' . $class_name . ' is not a child of ' . static::$base_class
    23            );
    24        }
    25
    26        // new object
    27        return new $class_name;
    28
    29    }
    30
    31}
    

    A change so small allows us to create a much “nicer” factory class:

     1class DrinkFactory extends absFactory {
     2
     3     static protected $base_class = 'Drink';
     4
     5}
     6
     7try {
     8
     9    $obj = DrinkFactory::getInstance('Tea');
    10
    11} catch (Exception $e) {
    12
    13    echo $e->getMessage();
    14}
    

    Basically in this version only the relevant property in this context is overwritten.

  • Factory method design patten in PHP

    Citește postarea în română

    Jan 23, 2010 Design patterns PHP
    Share on:

    An creational design pattern, which represents an solution for generation objects without specifying the class name. Virtually all methods that generate objects are particular implementations of the Factory Method pattern.

    Motivation:

    A motivation for this choice is that the creation of the object is delegated to the factory method.

    Diagram:

    factory method pattern

    Implementation:
    The classes that generates the objects, the common class to extend and the class with the factory method:

      1// the common class to be extended
      2abstract class OutputBase {
      3     // output the necessary headers
      4     abstract public function header();
      5
      6     // resulting body
      7     abstract public function body($data);
      8
      9}
     10
     11// class for XML ouput
     12class XMLOutput extends OutputBase {
     13
     14     // output XML header
     15     public function header() {
     16          header('content-type: text/xml');
     17     }
     18
     19     // xml document
     20     public function body($data) {
     21          $res = '';
     22
     23          $res .= '';
     24
     25          foreach ($data as $root => $item) {
     26
     27               $res .= '';
     28
     29               foreach ($item as $key => $val) {
     30                    $res .= '<'.$key.'>'.$val.'';
     31               }
     32
     33               $res .= '';
     34          }
     35
     36          $res .= '';
     37
     38          return $res;
     39
     40     }
     41
     42}
     43
     44// class for CSV output
     45class CSVOutput extends OutputBase {
     46
     47     // output CSV header
     48     public function header() {
     49          header("Content-type: text/plain");
     50     }
     51
     52     // CSV body
     53     public function body($data) {
     54          $res = '';
     55
     56          $keys = array_keys($data[0]);
     57
     58          foreach ($keys as $key) {
     59               $res .= '"'.$key.'";';
     60          }
     61          $res .= "\r\n";
     62
     63          foreach ($data as $item) {
     64               foreach ($item as $val) {
     65                    $res .= '"'.$val.'";';
     66               }
     67
     68               $res .= "\r\n";
     69          }
     70
     71          return $res;
     72     }
     73}
     74
     75// the factory method class
     76// is abstract so it wont be instantiated
     77abstract class OutputFactory {
     78
     79     // constant for XML type
     80     const XML = 1;
     81
     82     // constant for CSV type
     83     const CSV = 2;
     84
     85     // static factory method
     86     public static function getInstance($type) {
     87          // depending which constant was received as a parameter
     88          // one of the objects will be returned
     89          switch ($type) {
     90               case self::XML :
     91                    return new XMLOutput();
     92               break;
     93
     94               case self::CSV :
     95                    return new CSVOutput();
     96               break;
     97          }
     98
     99          // if the value received as a parameter is not one of the constants
    100          // an exception will be thrown
    101          throw new Exception('Invalid class type!');
    102     }
    103
    104}
    

    Example:

     1// the data
     2$data = array(
     3               array(
     4                    'a' => 1,
     5                    'b' => 2,
     6                    'c' => 3
     7                    ),
     8               array(
     9                    'a' => 4,
    10                    'b' => 5,
    11                    'c' => 6
    12                    )
    13          );
    14
    15// try-catch block in case of an exception
    16try {
    17     // generation the object
    18     $obj = OutputFactory::getInstance(OutputFactory::XML);
    19
    20     // output headers
    21     $obj->header();
    22
    23     // display body
    24     echo $obj->body($data);
    25
    26} catch (Exception $e) {
    27
    28     $e->getMessage();
    29
    30}
    
    • ««
    • «
    • 13
    • 14
    • 15
    • 16
    • 17
    • »
    • »»

Claudiu Perșoiu

Programming, technology and more
Read More

Recent Posts

  • Adding a slider to Tasmota using BerryScript
  • The future proof project
  • Docker inside wsl2
  • Moving away from Wordpress
  • Custom path for Composer cache
  • Magento2 and the ugly truth
  • A bit of PHP, Go, FFI and holiday spirit
  • How to make use of the Xiaomi Air Conditioning Companion in Home Assistant in only 20 easy steps!

PHP 49 MISCELLANEOUS 46 JAVASCRIPT 14 MAGENTO 7 MYSQL 7 BROWSERS 6 DESIGN PATTERNS 5 HOME AUTOMATION 2 LINUX-UNIX 2 WEB STUFF 2 GO 1

PHP 35 JAVASCRIPT 15 PHP5.3 11 MAGENTO 7 PHP6 7 MYSQL 6 PHP5.4 6 ZCE 6 CERTIFICARE 5 CERTIFICATION 5 CLOSURES 4 DESIGN PATTERNS 4 HACK 4 ANDROID 3
All tags
3D1 ADOBE AIR2 ANDROID3 ANGULAR1 ANONYMOUS FUNCTIONS3 BERRYSCRIPT1 BOOK1 BROWSER2 CARTE1 CERTIFICARE5 CERTIFICATION5 CERTIFIED1 CERTIFIED DEVELOPER1 CHALLENGE1 CHM1 CLASS1 CLI2 CLOSURES4 CODE QUALITY1 CODEIGNITER3 COFFEESCRIPT1 COLLECTIONS1 COMPOSER1 CSS1 DEBUG1 DESIGN PATTERNS4 DEVELOPER1 DEVELOPMENT TIME1 DOCKER2 DOCKER-COMPOSE1 DOUGLAS CROCKFORD2 ELEPHPANT2 FACEBOOK2 FFI1 FINALLY1 FIREFOX3 GAMES1 GENERATOR1 GO1 GOOGLE1 GOOGLE CHROME1 GOOGLE MAPS1 HACK4 HOMEASSISTANT2 HTML2 HTML HELP WORKSHOP1 HTML51 HUG1 HUGO1 INFORMATION_SCHEMA1 INI1 INTERNET EXPLORER3 IPV41 IPV61 ITERATOR2 JAVASCRIPT15 JQUERY1 LAMBDA1 LINUX1 MAGENTO7 MAGENTO22 MAP1 MINESWEEPER1 MOTIVATION1 MYSQL6 NGINX1 NODE.JS2 NOSQL1 OBSERVER3 OBSERVER PATTERN1 OOP1 OPERA1 OPTIMIZATION1 ORACLE1 PAGESPEED1 PAIR1 PARSE_INI_FILE1 PHONEGAP2 PHP35 PHP ELEPHANT2 PHP FOR ANDROID1 PHP-GTK1 PHP42 PHP53 PHP5.311 PHP5.46 PHP5.53 PHP5.61 PHP67 PHP7.41 PROGRAMMING1 REVIEW1 ROMANIAN STEMMER2 SAFARY1 SCALAR TYPE HINTING1 SCHEME1 SET1 SHOPPING CART PRICE RULE1 SINGLETON1 SOAP1 SPL2 SQLITE1 SSH1 STACK TRACE1 STDERR1 STDIN1 STDOUT1 SUN1 SYMFONY2 TASMOTA1 TEST TO SPEECH1 TITANIUM2 TRAITS1 TTS1 UBUNTU1 UNICODE2 UTF-82 VECTOR1 WEBKIT1 WINBINDER1 WINDOWS2 WORDPRESS1 WSL21 YAHOO3 YAHOO MAPS1 YAHOO OPEN HACK1 YSLOW1 YUI1 ZCE6 ZCE5.31 ZEND3 ZEND FRAMEWORK3
[A~Z][0~9]

Copyright © 2008 - 2024 CLAUDIU PERȘOIU'S BLOG. All Rights Reserved