Hi,
To get to know the PHP syntax and functions faster I'd recommend downloading the manual:
http://www.php.net/download-docs.php
Some of the things you'll have to look at first in PHP manual is how it handles datatypes. PHP types are not explicitly defined. For example, you can have a condition that matches a string against an integer. The string will be treated as the integer value. eg: if ('1' == 1) is TRUE. To compare types, you need to use ===.
Heres a google search for "PHP OOP" that may help with OOP:
http://www.google.com.fj/search?q=PHP+OOP
PHP4 and PHP5 have unique differences in approaching OOP. You'll have to choose which you are targeting or stick to what works for both.
PHP has shorthand syntax support, but it has to be enabled in the configuration, which is found in the file PHP.ini. (you can google that) so I recommend sticking to regular PHP syntax.
eg:
[PHP]
// short
<?php= $variable; ?>
// long
<?php echo $variable; ?>
[/PHP]
The "common" shorthand syntax such as:
[PHP]<?php
// short
$variable = (condition) ? 'value if true' : 'value if false';
// long
if (condition) {
$variable = 'value if true';
} else {
$variable = 'value if false';
}
// etc...
?>[/PHP]
exists in PHP without any extensions or configuration.
Heres the PHP4 OOP docs:
http://www.php.net/oop
I assume you know the basic syntax for creating a class (Object) in PHP.
Here's a simple class as an example:
[PHP]
class className {
/** statically define a property of className */
var $property;
/**
* constructor - same name as Class
* This will be called whenever a new Object is created with the "new className()" syntax
*/
function className() {
// $this refers the this class Instance - when called from an Object created with "new className()".
$this->property = 1;
}
/** method of className */
function method1() {
$this->property = 2;
}
/** method of className */
function method2() {
return $this->property;
}
}
[/PHP]
To view the methods of this class you use:
[PHP]get_class_methods('className')[/PHP]
This returns an array of methods of the class. You can dump the array using var_dump().
To create an Object from the class you use:
[PHP]<?php
$myObject = new className();
?>[/PHP]
The object $myObject will have the methods: className(), method1(), method2() and the property "property".
The correct syntax for referencing a method is:
[PHP]$myObject->method1();[/PHP]
for referencing a property:
[PHP]$myObject->property;[/PHP]
To get the list of methods:
[PHP]get_class_methods($myObject)[/PHP]
Note that you are passing an object to get_class_methods(). This returns the methods of the object. If you pass a string such as "className" as done previously, it returns the methods of the class defined statically.
To create a class that extends the class "className" use the "extends" keyword:
[PHP]
class myClass extends className {
/** Statically set $myproperty */
var $myproperty = 1;
function myMethod() {
return array($this->property, $this->myproperty);
}
}[/PHP]
Now if you take a look at the properties and methods of an Object Instance of myClass, it will have inherited those of className also.
eg:
[PHP]
// create Object - Instance of myClass
$myObject2 = new myClass();
// notice that it has the methods/properties of className also
var_dump( get_class_methods($myObject2) );
echo '<br />';
var_dump( get_object_vars($myObject2) );
[/PHP]
One thing to note is that inheritance goes only one way. myClass inherits the methods and properties of className but className does not inherit anything. So php makes you have to kind of program backwards since the parent, that the child inherits from, knows nothing of the child.
Another way of inheriting an Objects methods/properties is to make it a property of another Object dynamically.
eg:
[PHP]$myObject->property2 = $myObject2;[/PHP]
Then you can call the methods of $myObject2 like such:
[PHP]$myObject->property2->myMethod();[/PHP]
I like using this personally, as an Object can inherit from many other Objects without having to statically write "extends" into every class you want to inherit.
Note: This is based on PHP4.