------------------------------------------------------------
PHP and the Zend Engine - Past, Present, and into the Future
By: Ryan Gantt (For the Bozeman Linux User Group)
------------------------------------------------------------

What is PHP?

PHP is a widely-used general-purpose scripting language that is especially suited for 
Web development and can be embedded into HTML.

How can PHP be used?
<html>
<head>
	<title>A Page</title>
<#META#>
</head>
<body>
<?
for($i = 0; $i < 10; $i++)
{
	echo $i;
}
$body = str_replace("<#META#>", function(), $body);
?>
</body>
</html?

All of the following syntax allowed:

if(){}
if():
if()
if() endif;

Can be applied to if, switch, for, foreach, while, do/while

- PHP can be embedded into HTML
- PHP can be embedded ANYWHERE into HTML
	- e.g.  <span style="<?=$style1?>" width="<?=$width?>">
	- e.g.  $var = "itle"; <t<?=$var?>>A New Page</t<?=$var?>>
- This can happen because the php output is turned into HTML BEFORE the page is displayed, not WHILE its being displayed

---------------------------------
History of PHP:
_________________________________

PHP/Form Interpreter 1 & 2
------ 
- Created by Rasmus Lerdorf in 1995 (Making PHP 8 years old)
- Initially as a simple set of Perl (Fate, it seems, is not without a sense of irony) scripts for tracking accesses to his online resume. 
- Named 'Personal Home Page Tools'. 
- Rasmus wrote a much larger C implementation
	- Able to communicate with databases, 
	- Enabled users to develop simple (simple is the keyword) dynamic Web applications. 

- Perl-like variables/Type Indifference -- PHP is still Loosely typed
- Automatic interpretation of form variables -- HTTP_POST_VARS, HTTP_GET_VARS
- HTML embedded syntax -- <? ?>

Rasmus chose to release the source code for PHP/FI for everybody to see, so that anybody can use it, as well as fix bugs in it and improve the code. 

1997 -- PHP/FI 2.0
- Several thousand users around the world 
- Approximately 50,000 (actually quite sad) domains reporting as having it installed (1%)
- A one-man project. 



---------------------------------
PHP/FI 2.0 was officially released only in November 1997

_________________________________

PHP 3.0 (Enter Zeev & Andi)
-------
- PHP 3.0 was the first version that closely resembles PHP 4 stable as we know it today.
- Created by Andi Gutmans and Zeev Suraski in 1997
- Complete rewrite
- PHP/FI 2.0 severely underpowered for semi-enterprise applications
- Andi, Rasmus and Zeev announced PHP 3.0 as successor of PHP/FI 2.0.

- One of the biggest strengths of PHP 3.0 were its' strong extensibility features. 
- Provided end users with an infrastructure for many different databases, protocols and APIs	- Key to PHP 3.0's tremendous success
	- Developer extension modules
	- Object oriented syntax support 
	- Consistent language syntax. 

- new name removed the implication of limited personal use
- Recursive acronym - PHP: Hypertext Preprocessor. 

Late 1998
- PHP grew to a base of tens of thousands of users
- Hundreds of thousands of Web sites reported having it installed
- At its peak, PHP 3.0 was installed on approximately 10% of Web servers 

PHP 3.0 was officially released in June 1998, after having spent about 9 months in public testing.



----------------------------------------
PHP 4.0 (The dubbing of the Zend Engine) (Zend = ZEev & aNDi)

________________________________________

Winter 1998
- Andi Gutmans and Zeev Suraski had begin working on a rewrite of PHP's core
- Goals: 
	- Improve performance of complex applications
	- Improve the modularity of PHP's code base

- Such applications were made possible by PHP 3.0's new features 
	- Support for a variety of databases
	- Support for third party APIs

- PHP 3.0 was not designed to handle such complex applications efficiently. 

The 'Zend Engine' (comprised of their first names, Zeev and Andi), met these design goals successfully
----------------
- First introduced in mid 1999
- PHP 4.0 was based on this engine
	- Officially released in May 2000

- Highly improved performance
- Support for many more Web servers
- HTTP sessions
- $_GET, $_POST, $_COOKIE, $_SERVER, $_SESSION --  With register globals on
- Output buffering
- More secure ways of handling user input 

- PHP 4 was NOT very much different in terms of Object Model than PHP 3.
	- Objects were still 'syntactic suger' for accessing associative arrays.
	- Objects were plainly thought of as 'containers' of properties.
	- The only REAL difference in PHP 4 was that objects had to be based off of a class.
		- Methods, Members
		- Constructors -- Must be named the same as the class they are in
		- Instantiated with the 'new' keyword
		- Single Inheritance

	- Objects allowed for calling of methods within or from outside the object contexts:: $this-> (current context)
	 and $obj-> (instance)

:: php|arch article on the new object model by Zeev Suraski:
	".. despite the very limited functionality, and despite a host of problems and limitations, object oriented
	 programming in PHP thrived and became the most popular paradigm for the growing numbers of off-the-shelf 
	 applications. This trend, which was mostly unexpected, caught PHP in a sub-optimal situation."



PHP 4.3.2RC2 is currently the latest released version of PHP. Work has already begun on modifying and improving the Zend Engine to integrate the features which were designed for PHP 5.0. 

Today, PHP is being used by hundreds of thousands of developers (estimated), and several million sites report as having it installed, which accounts for over 20% of the domains on the Internet. 

PHP's API includes the following, which are BUILT IN:

Apache, Bzip, Calendar, COM, Class/Object, PDF, SAX, DOM, Expat, Databases( MySQL, PgSQL, U/ODBC, mSQL, msSQL, dBase, Oracle, Oracle8, interbase),
cURL (ClientURL), Date/Time, .NET, FTP, HTTP, GMP, IMAP, POP3, NNTP, Java, NCurses, OpenSSL, Regex, Posix, PSpell, RPC, XSLT, Session,
Socket, Stream, Tokenizer, Zip, and Zlib (as of PHP 4.3.2RC2)


-----------------------------------
PHPs' Limitations and the Angry Mob

___________________________________
- There is no angry mob
- The issue: PHP was not designed as an OOPL -- majority of end developers did it anyway (Including Me =])
- Objects in PHP are treated like any primitive datatype
- All primitive datatypes are passed by value, so every object passed was also duplicated
	- Duplication is usually not the desired effect in an OOPL
	- Duplication costs LOTS of memory
	- Force non-duplication makes horrible looking code

- Objects are nothing more than an associative array

Notable limitations in Zend 1.0
--------
- Object support
	- Passing by reference
	- Returning by reference
	- Object copying
	- Deletion of objects:
		- Destructors
		- Garbage collection
		- unset()

	- Object cloning
	
- Access levels
- Error handling 
- Third party API support
	- Java objects
	- COM objects
	- CORBA objects 

A typical scenario in PHP 4:

<?
function &aMethod( &$objectVar )
{
	$this->var =& $objectVar;
	return $this->var->bMethod();
}
?>

Now let's look at how we do it in PHP 5:

<?
public function aMethod( DataType $objectVar )
{
	if( $objectVar instanceof DataType )
	{
		return $objectVar->bMethod();
	}
}
?>

______________________________________

Zend 2 and the birth of PHP 5:
---------

How Zend 2 can solve some of these issues:
- Object support
	- Passing by reference
	- Returning by reference
	- Object copying
	- Object returning
	- Method dereferencing	
	- String reference Syntax

	PHP 4:
	All objects are passed by value (copied), unless implicitly told to do otherwise with &
	
	PHP 5:
	When an object is instantiated, it is given a unique id, or 'handle'
	When you pass the object, only the handle is passed; there remains still only one object in memory
	All methods can access and modify the 'handle''s target, either directly or with accessors/mutators

	- Deletion of objects
		- Destructors
		- Garbage collection
		- unset

	PHP 4:
	The only way to 'get rid of' an object was to unset() it, which i'm convinced did nothing

	PHP 5:
	defining the __destruct() method in a class allows you to manually make final adjustments to the class
	the 'delete' keyword can be used force the deletion of an object, even if there are still references to it

	- Object cloning

	PHP 4:
	Zend does a bitwise copy, making an identical replica of all the object's properies

	PHP 5:
	A user defined __clone() method will allow an end developer to choose what copy constructor to run while cloning

	PHP 4:
	When you return objects from a method, you must use an & when you set the method to a value, ex// $foo = &$bar();
	and it is impossible to return an object by reference

	PHP 5:
	Methods that return objects, such as factory patterns, are made easy, because of the improved OO API
	
	e.g.
	public function factory( String $var )
	{
		switch($var)
		{
			case 'foo':
				$obj = new Foo('Bar');
				break;
			case 'bar':
				return new Bar('Foo');
				break;
			default:
				throw new DataTypeException("Datatype ".get_class($var)." does not exist!");
		}
	}
			
	PHP 4:
	To access an offset of a string, you would use the array access method, e.g. $string[0] = 'a'.
	This works, but if the $string variable hasn't been initialized, then it creates a new array called $string,
	which is definately not what the author had intended for it to do.

	PHP 5:
	PHP 5 offers a clever alternative to this conundrum, by using the following syntax: $string{0}, where 0 is the offset.
	This appears the same as the original method, however, it will only access that offset if the offset exists,
	otherwise it will throw an Exception. This allows developers and maintainers of code to realize the original
	intention of the author, to use arrays or string offsets. While not ALL TOO important to actual language
	survival, it is a nice bell to add to all the whistles.

--------------------------------------------------------------

Random features, not as noteworthy, but still worthy of a note:
:: the import statement:
	- Use the import statement to extract methods from classes
:: namespaces
	- Holds classes and functions, and everything you need
	- Creates an artificial 'scope' for your program
	- Classes and functions can be used externally with Namespace::class::method() syntax.
:: public, private, and protected members and functions
	- Limit access to those lesser mortals implementing your classes!
:: abstract classes and interfaces
	- Create a guide for your hierarchy without actually writing the implementation of your methods.
:: final keyword
	- Create constant methods that cannot be overriden down the hierarchy.
:: constant members
	- Create constants without the use of define();
:: static methods and members
	- Create methods and members that can be accessed without creating an instance!
:: instanceof
	- Check if a passed object is an instance of a specified class!
:: Default parameter values
	- Not sure what changed... I use this feature in PHP 4 all the time..
:: __autoload()
	- Used for creating a lambda class


SUPER CODE SNIPPETS!:

PHP 4: