Categories
GNU/Linux Free Software & Open Source Programming & Web Development

How to install PHP PDO extensions on Debian Lenny

php

If you need to install PHP PDO extensions on Debian Lenny, its very
simple. This is what I did to get it done:

sudo aptitude install php5 php5-dev php5-cli libmysqlclient15-dev

Then use pecl to get PDO:

sudo pecl install pdo

Then, in case you want the PDO MySQL extension:

sudo pecl install pdo_mysql

update: I found out that PDO and mysqli drivers conflicted on my system and I ended up with none. To fix this, do pecl install mysql and then add extension=mysql.so on your php.ini like indicated below. This enables pdo and mysql driver, but I couldn’t get msyqli driver back on.

Then you have to edit /etc/php5/apache2/php.ini and add:

extension=pdo.so

Also add this line only if you installed pdo_mysql.

extension=pdo_mysql.so

Restart your web server with:

sudo invoke-rc.d apache2 restart

And that’s it. Enjoy your PDO extensions on Debian Lenny.

Categories
Emacs GNU/Linux Free Software & Open Source

PHP syntax error check as you type with Emacs

Emacs php syntax checking on the fly

For those who have to code in PHP, there’s a nice feature in Emacs that makes your coding horror times less stressing and helps you avoid typos and similar dumb errors. For example in the image above, I missed the colon at the end of the line.

Emacs 22.1 comes with flymake mode, a nice tool that makes syntax checking while you type out the file by highlighting the lines with errors and displays the error messages.

You can enable flymake to check PHP syntax by adding the following code on your .emacs or whatever Emacs customizations file you use:

;; Flymake PHP Extension
(require 'flymake)
(unless (fboundp 'flymake-php-init)
  (defun flymake-php-init ()
    (let* ((temp-file (flymake-init-create-temp-buffer-copy
		       'flymake-create-temp-inplace))
	   (local-file (file-relative-name
			temp-file
			(file-name-directory buffer-file-name))))
      (list "php" (list "-f" local-file "-l")))))
(let ((php-ext-re "\\.php[345]?\\'")
      (php-error-re
       "\\(?:Parse\\|Fatal\\) error: \\(.*\\) in \\(.*\\) on line \\([0-9]+\\)"))
  (unless (assoc php-ext-re flymake-allowed-file-name-masks)
    (add-to-list 'flymake-allowed-file-name-masks
		 (list php-ext-re
                       'flymake-php-init
                       'flymake-simple-cleanup
                       'flymake-get-real-file-name))
    (add-to-list 'compilation-error-regexp-alist-alist
		 (list 'compilation-php
                       php-error-re  2 3 nil nil))
    (add-to-list 'compilation-error-regexp-alist 'compilation-php)
    (add-to-list 'flymake-err-line-patterns
		 (list php-error-re 2 3 nil 1))))
;; add php flymake support
(add-hook 'php-mode-hook (lambda () (flymake-mode t)))

Its very nice to have on the fly syntax checking.

Categories
GNU/Linux Free Software & Open Source Programming & Web Development

PHP 5 OOP implementation quirks

PHP 5 is supposed to be fully object oriented. Yes, it has classes and inheritance and all those things an OOP language is supposed to have. But, is it implemented like most OOP languages?

First of all, I come from a C/C++ background, so there’s where I got all my OOP lessons. Then I started to learn Python and Ruby, and well, finally lots of little OOP details I couldn’t understand were clear. Ruby has a beautiful OOP implementation, sometimes it can get confusing having everything as an object and the object and class themselves are objects. But once you pass that, everything is cystal clear.

Constructors

What bothers me about PHP 5’s OOP implementation is first of all, constructors. If I declare a Parent class, with a constructor, then a Child class that inherits from Parent, and this Child class has its constructor, when I instantiate an object of the Child class, only the Child class constructor is called.

class Parent
{
  __construct()
   {
      echo "hello from parent ";
   }
}
class Child extends Parent
{
 __construct()
   {
      echo "hello from child ";
   }
}
dummy = new Child();

This will display only

hello from child

Where in other languages, such as C++, Python or Ruby (don’t know about Java, but I expect the same) the output would be:

hello from parent hello from child

So in order to have this constructor cascading effect, in PHP 5 I have to explicitly call the parent constructor on my child’s constructor like this:

class Child extends Parent
{
 __construct()
   {
      echo "hello from child ";
      parent::__construct();
   }
}

Which is not very nice.

Accessor methods

Another aspect that I dislike is the class attribute accessor methods. I call them accessors like in Ruby, but they are commonly called setter and getter methods. So in order to control and filter my input/output for my object, I usually do so with this methods.

If I declare a User class with some methods, I expect the setter and getter methods to filter my I/O everytime I want to access the class’ attributes.

class User
{
   public $name;
   public $email;
   public $password;
 //set and get functions
  public function __get($variable)
    {
      return $this->$variable;
    }
  public function __set($var, $value)
    {
      trim($value);
      if($var != "password")
        {
          $this->$var = $value;
        }
      else
        {
          $this->password = hash('md5',$value);
        }
    }
}

This is supposed to encrypt the password everytime I set something on my password object attribute. What is wrong with this? That it doesn’t work! The get and set functions only work when you have NO attributes or undefined attributes in the class. So in order for this getter and setter methods to really filter I/O to the instantiated objects is to not declare the attributes, and this methods will generate them, like the PHP people say, “magically”. Maybe its a nice flexibility feature to have attributes generated on-the-fly, but it doesn’t help on object consistency.

PHP 5 is a nice tool, but as a language I think there’s still work to do on the OOP implementation.