<?xml version="1.0" encoding="iso-8859-1"?>
<feed xmlns="http://www.w3.org/2005/Atom"
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      xml:lang="en">
<title>Victor&#039;s Blog about PHP, Zend Framework &amp; Cake PHP</title> 
<link rel="alternate" type="text/html" href="http://www.victorsawma.com/2_victors_blog_about_php_zend_framework__cake_php" /> 
	 
	<updated>2010-11-16T07:24:38+00:00</updated> 
<generator>lifetype-1.2.10_r6971</generator> 
<id>http://www.victorsawma.com/2_victors_blog_about_php_zend_framework__cake_php/feeds/atom</id>
 
<rights>Copyright (c) victor</rights> 
  
 <entry> 
 <id>tag:www.victorsawma.com,2010-11-16:119</id>
 <title>PHP Function Overloading</title> 
 <link rel="alternate" type="text/html" href="http://www.victorsawma.com/2_victors_blog_about_php_zend_framework__cake_php/archive/119_php_function_overloading.html" /> 
  
 <updated>2010-11-16T07:24:38+00:00</updated> 
 <summary type="text"> 
All Object Oriented languages (or almost all of them) support function overloading where programmers are allowed to define multiple functions with the same name but with separate &amp;quot;function ...</summary> 
 <author> 
  
 <name>victor</name> 
</author> 
<dc:subject>
OOPHP 
</dc:subject> 
 <content type="text" xml:lang="en" xml:base="http://www.victorsawma.com/2_victors_blog_about_php_zend_framework__cake_php"> 
  
All Object Oriented languages (or almost all of them) support function overloading where programmers are allowed to define multiple functions with the same name but with separate &quot;function signatures&quot;. A function signature is usually the number of parameters, the order of parameters and / or the type of parameters.
 
 
For example, in Java, you are allowed to define the following:
 
 
 public void testFunction (int x) { ... } 
 
 
 public void testFunction (int x, int y) { ... } 
 
 
 public void testFunction (int x, double y) { ... } 
 
 
By defining the above, the Java Virtual Machine will automatically invoke the appropriate function once called based on the number and type of parameters given. For example, invoking
 
 
 testFunction (2); 
 
 
will automatically invoke the first function while invoking
 
 
 testFunction (2, 3.1); 
 
 
will automatically invoke the third function.
 
 
PHP, on the other hand, does not have support for function overloading yet. Defining the same function more than once with a different number of paramters will surely (so far) generate an error at the parser level.
 
 
It is still possible to simulate function overloading in PHP using parameters default values. Upon function definition, simply provide default values to parameters as follows:
 
 
 function testFunction ($a = false, $b = false, $c = false) { .... } 
 
 
By doing so, you will be defining a function that takes 3 arguments ($a, $b, and $c) whose default value is false.
 
 
Within the function body, you will then simply check whether the value of the parameter that is being passed is still false and act accordingly.
 
 
Remember, though, that PHP provides automatic data type conversion. In other words, if the value of $b is 0 (Zero) and you check whether $b is false using the equality operator (==), you will get a true value returned. As such, remember to use the data typed equality operator (===) instead of the normal operator (==).
 
 
&nbsp;
  
</content> 
</entry> 
 
 <entry> 
 <id>tag:www.victorsawma.com,2010-02-22:111</id>
 <title>PHP Autoloading</title> 
 <link rel="alternate" type="text/html" href="http://www.victorsawma.com/2_victors_blog_about_php_zend_framework__cake_php/archive/111_php_autoloading.html" /> 
  
 <updated>2010-02-22T09:20:34+00:00</updated> 
 <summary type="text"> 
Autoloading is the process of automatically loading classes when needed wthout having to go through the traditional include() and require() directives. This helps PHP developers work without ...</summary> 
 <author> 
  
 <name>victor</name> 
</author> 
<dc:subject>
OOPHP 
</dc:subject> 
 <content type="text" xml:lang="en" xml:base="http://www.victorsawma.com/2_victors_blog_about_php_zend_framework__cake_php"> 
  
Autoloading is the process of automatically loading classes when needed wthout having to go through the traditional include() and require() directives. This helps PHP developers work without having to worry about whether the class that they need has already been loaded or not.
 
 
To achieve this, PHP 5 provides the __autoload() magic method that is called automatically whenever a class / object is being referenced while not being defined. If a definition exists for that class, the magic method will not be invoked. If a definition does not exist, the __autoload() method will be invoked before giving up and generating a warning / error message.
 
 
So how does auto loading work in examples?
 
 
The steps are pretty simple:
 
 
	 Define a function __autoload($class) that will be automatically passed the name of the class that is being loaded. 
	 Provide an implementation for that function that tries to search / find / load the class as needed by the application.  
 
 
Take the following example to be able to better explain this point.
 
 
Given fhe following directory structure within an application:
 
 
app |
.......| public
............| index.php
............| images
...................| ...
............| style
...................| ...
.......| library
............| Db
...................| Mysql.php
...................| Oracle.php
............| Output
...................| Xhtml.php
...................| Xml.php
...................| Ajax.php
 
 
if we try to load the mysql.php file&nbsp; into index.php, our PHP code would normally look as follows:
 
 
 include(ROOTDIR . &quot;/app/library/Db/Mysql.php&quot;); 
 
 
Assuming that ROOTDIR is a defined constant pointing to the root directory of our application, this statement will ask the PHP interpreter to load the file mysql.php from the given path.
 
 
The same will need to be done everytime we need to use any file from within the library folder. This, however, can be automagically done through the __autoload() magic function that was described above. The code below is what can be used and I will explain, after it, its many advantages.
 
 
 
 function __autoload($className) { 
&nbsp;&nbsp;&nbsp; $filePath = str_replace(&quot;_&quot;, &quot;/&quot;, $className); 
&nbsp;&nbsp;&nbsp; $fileName = LIBRARYDIR . '/' . $filePath . &quot;.php&quot;; 
&nbsp;&nbsp;&nbsp; require_once $fileName; 
} 
 
 
 
The code above is taking the class name ($className) and replacing all occurrences with '_' with a '/' to build up the complete path to the file. If our class is named Db_Mysql, the__autoload() function will automatically build the path to the file and load it. 
 
 
Inside the index.php file, we will simply use our class without having to include it first but we will need to pay attention to how we name our classes
 
 
An example of how the index.php file will look like will follow and is explained in details:
 
 
&nbsp;
 
 
 function __autoload($className) { 
  &nbsp;&nbsp;&nbsp; $filePath = str_replace(&quot;_&quot;, &quot;/&quot;, $className); 
&nbsp;&nbsp;&nbsp; $fileName = LIBRARYDIR . '/' . $filePath . &quot;.php&quot;; 
&nbsp;&nbsp;&nbsp; require_once $fileName;   
} 
 
$x = new Db_Mysql();  
 
When we try to create a new instance of Db_Mysql, the PHP interpreter will notice that it is not loaded and will automatically invoke the __autoload() function passing it Db_Mysql as the class name.
 
&nbsp;
 
 
$filePath will hold the value Db/Mysql
 
 
$fileName will have the value&nbsp; &lt;path to application&gt;/app/library/Db/Mysql.php
 
 
and the file will be automatically required.
 
 
It is worth noting that as of PHP 5.3, you can try to place the autoload() magic method within a try { } catch block. Any exception thrown while trying to load the file can be catched and, thus, different locations can be provided for autoloading.
 
 
Also, another nice feature is available with the Zend Autoloader which provides namespaces for loading objects. In other words, you can define many locations to be used for autoloading and the Zend Autoloader will search these locations every time a new object is being loaded. 
  
</content> 
</entry> 
 
 <entry> 
 <id>tag:www.victorsawma.com,2010-02-21:110</id>
 <title>What is PHP, Zend Framework and Cake PHP?</title> 
 <link rel="alternate" type="text/html" href="http://www.victorsawma.com/2_victors_blog_about_php_zend_framework__cake_php/archive/110_what_is_php_zend_framework_and_cake_php.html" /> 
  
 <updated>2010-02-21T01:33:12+00:00</updated> 
 <summary type="text"> 
My first really mini-article will be simply a definition of the 3 topics behind this blog.
 
 
 PHP is a widely used, general-purpose web scripting language that is embedded into the HTML ...</summary> 
 <author> 
  
 <name>victor</name> 
</author> 
<dc:subject>
Cake PHP 
Zend Framework 
OOPHP 
</dc:subject> 
 <content type="text" xml:lang="en" xml:base="http://www.victorsawma.com/2_victors_blog_about_php_zend_framework__cake_php"> 
  
My first really mini-article will be simply a definition of the 3 topics behind this blog.
 
 
 PHP is a widely used, general-purpose web scripting language that is embedded into the HTML source document and interpreted by a web-server integrated PHP processor module. 
 
 
The name &quot;PHP&quot; is a recursive acronym that stands for &quot;PHP Hypertext Preprocessor&quot;. PHP code can be also processed by an interpreter application in command-line mode to perform desired operating system operations. It may also function as a graphical application through the integration of a windowing toolkit (like PHP GTK).PHP is available as a processor for most modern web servers and as standalone interpreter on most operating systems and computing platforms. 
 
 Zend Framework (ZF) is an open source, object-oriented web application framework implemented in PHP 5. ZF is a use-at-will framework. 
 
 
There is no single development paradigm or pattern that all Zend Framework users must follow, although ZF does provide components for the (Model View Controller paradigm) MVC, Table Data Gateway, and Row Data Gateway design patterns. Zend Framework provides individual components for many other common requirements in web application development.
 
 
 CakePHP is an open source web application framework for producing web applications. It is written in PHP and is compatible with PHP 4 and PHP 5. 
 
 
CakePHP makes it easier for the user to interface with the database with the active record pattern. It also encourages the use of the MVC architectural pattern and provides cool features like integrated CRUD for datasource interaction, application scaffolding, built-in validation, data sanitization, and internationalization and localization, various behaviors, components and helpers to minimize development time, as well as unit testing using the SimpleTest framework. 
 
PHP, Zend Framework and CakePHP are all available through open-source licenses that allow for their free usage (although dontaion is highly recommended to maintain them) and that provide complete access to the source code behind them. More detailed topics pertaining to specific characteristics of these technologies will be provided in future articles.
  
</content> 
</entry> 
 
 <entry> 
 <id>tag:www.victorsawma.com,2010-02-21:109</id>
 <title>My PHP Blog</title> 
 <link rel="alternate" type="text/html" href="http://www.victorsawma.com/2_victors_blog_about_php_zend_framework__cake_php/archive/109_my_php_blog.html" /> 
  
 <updated>2010-02-21T01:24:27+00:00</updated> 
 <summary type="text">In this blog, I will be writing specialized articles about PHP, Zend Framework, Cake PHP and other PHP-related topics as I come across them. The main point behind this blog is two-fold: sharing my ...</summary> 
 <author> 
  
 <name>victor</name> 
</author> 
<dc:subject>
General 
</dc:subject> 
 <content type="text" xml:lang="en" xml:base="http://www.victorsawma.com/2_victors_blog_about_php_zend_framework__cake_php"> 
 In this blog, I will be writing specialized articles about PHP, Zend Framework, Cake PHP and other PHP-related topics as I come across them. The main point behind this blog is two-fold: sharing my findings with the community and receiving feedback in return. If you have any topic that you wish to discuss, please contact me using the contact form. I will also really appreciate having your feedback. Sharing knowledge is the key to mastering topics afterall. 
</content> 
</entry> 
 
</feed>
