PHP
  Home arrow PHP arrow Page 3 - Overloading and Object-Oriented Programming with PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Overloading and Object-Oriented Programming with PHP 5
By: Sams Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 11
    2006-10-12


    Table of Contents:
  • Overloading and Object-Oriented Programming with PHP 5
  • SPL and Interators
  • _ _call()
  • _ _autoload()

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Overloading and Object-Oriented Programming with PHP 5 - _ _call()
    ( Page 3 of 4 )

    PHP also supports method overloading through the _ _call() callback. This means that if you invoke a method of an object and that method does not exist, _ _call() will be called instead. A trivial use of this functionality is in protecting against undefined methods. The following example implements a _ _call() hook for a class that simply prints the name of the method you tried to invoke, as well as all the arguments passed to the class:

    class Test {
     public function _ _call($funcname, $args)
     {   
      print "Undefined method $funcname called with
    vars:\n"; print_r($args); } }

    If you try to execute a nonexistent method, like this:

    $obj = new Test;
    $obj->hello("george");

    you will get the following output:

    Undefined method hello called with vars:
    Array
    (
      [0] => george
    )

    _ _call() handlers are extremely useful in remote procedure calls (RPCs), where the exact methods supported by the remote server are not likely to know when you implement your client class. RPC methods are covered in depth in Chapter 16, "RPC: Interacting with Remote Services." To demonstrate their usage here briefly, you can put together an OO interface to Cisco routers. Traditionally, you log in to a Cisco router over Telnet and use the command-line interface to configure and maintain the router. Cisco routers run their own proprietary operating system, IOS. Different versions of that operating system support different feature sets and thus different command syntaxes. Instead of programming a complete interface for each version of IOS, you can use _ _call() to automatically handle command dispatching.

    Because the router must be accessed via Telnet, you can extend PEAR's Net_Telnet class to provide that layer of access. Because the Telnet details are handled by the parent class, you only need two real functions in the class. The first, login(), handles the special case of login. login() looks for the password prompt and sends your login credentials when it sees the password prompt.


    PEAR - PHP Extension and Application Repository (PEAR) is a project that is loosely associated with the PHP group. Its goal is to provide a collection of high-quality, OO, reusable base components for developing applications with PHP. Throughout this book, I use a number of PEAR classes. In both this book and my own programming practice, I often prefer to build my own components. Especially in performance-critical applications, it is often easiest to design a solution that fits your exact needs and is not overburdened by extra fluff. However, it can sometimes be much easier to use an existing solution than to reinvent the wheel.

    Since PHP 4.3, PHP has shipped with a PEAR installer, which can be executed from the command line as follows:

    > pear

    To see the full list of features in the PEAR installer you can simply type this:

    > pear help

    The main command of interest is pear install. In this particular case, you need the Net_Telnet class to run this example. To install this class, you just need to execute this:

    > pear install Net_Telnet

    You might need to execute this as root. To see a complete list of PEAR packages available, you can run this:

    > pear list-all

    or visit the PEAR Web site, at http://pear.php.net/.


    The second function you need in the Net_Telnet class is the _ _call() handler. This is where you take care of a couple details:

    • Many Cisco IOS commands are multiword commands. For example, the command to show the routing table is show ip route. You might like to support this both as $router->show_ip_route() and as $router->show("ip route"). To this end, you should replace any _ in the method name with a space and concatenate the result with the rest of the arguments to make the command.

    • If you call a command that is unimplemented, you should log an error. (Alternatively, you could use die() or throw an exception. Chapter 3 covers good error-handling techniques in depth.)

    Here is the implementation of Cisco_RPC; note how short it is, even though it supports the full IOS command set:

    require_once "Net/Telnet.php";
    class Cisco_RPC extends Net_Telnet {
     protected $password;
     function _ _construct($address, $password,$prompt=
    false) { parent::_ _construct($address); $this->password = $password; $this->prompt = $prompt; } function login() { $response = $this->read_until("Password:"); $this->_write($this->password); $response = $this->read_until("$this->prompt>"); } function _ _call($func, $var) { $func = str_replace("_", " ", $func); $func .= " ".implode(" ", $var); $this->_write($func); $response = $this->read_until("$this->prompt>"); if($response === false || strstr($response,
    "%Unknown command")) { error_log("Cisco command $func unimplemented",
    E_USER_WARNING); } else { return $response; } } }

    You can use Cisco_RPC quite easily. Here is a script that logs in to a router at the IP address 10.0.0.1 and prints that router's routing table:

    $router = new Cisco_RPC("10.0.0.1", "password");
    $router->login();
    print $router->show("ip route");



     
     
    >>> More PHP Articles          >>> More By Sams Publishing
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek