msgbartop
I will happily conduct a FREE basic web security scan for any genuine organization interested in my services to point out whether or not I can find vulnerabilities in your application. Just contact me.
Need a PHP Programmer, PHP staff or project manager? Contact me now.
msgbarbottom

27 Jan 09 A Beginner’s Guide to Object Oriented Programming (OOP) in PHP5 – Classes, Objects and Methods

***Guest Blogger***
The Personal Home Page (PHP) programming language provides a lightweight framework and foundation for Web application development. It runs on Apache (HTTP) Web server software amonst others, embedded into Hypertext Markup Language (HTML), passed through a Hypertext Preprocessor. As free, open source software – it has low implementation, maintenance and debugging costs. Its latest iteration called PHP5, has incorporated even more powerful object-oriented programming functionalities.

While PHP3 has rendered server-side scripts and PHP4 has established backward compatibility, PHP5 has improved object-oriented programming (OOP) as a cross-platform language. OOP bundles data and code into “objects”. PHP5 works better with object handles; passing or assigning by reference is not required.

PHP code is bounded as follows:

opening delimiter is “<?php” closing delimiter is “?>”

The ‘echo’ code displays, writes or outputs data. PHP5 code also allows for ‘comments’ which can instruct other developers about special characteristics of your programming using a beginning delimiter of “/*” and an ending delimiter of “*/”, or “//” or “#” for a single line.

Basic PHP5 code includes the following basic rules:

Variables begin with a ‘$’ followed by a letter or underscore ‘_’
Variables cannot begin with a number or “*”
Variable names are case sensitive, written as all lower case or capitalize words after first (i.e. “myWord”)
Variables don’t need to be declared before using them
Variables are local to their scope
Assignment designation is “=”
Use a semicolon ‘;’ at end of line

Basic data types are dynamic, including the following:

Integers
Floating-point (real) numbers
Strings (series of characters treated as a single unit of data)
Booleans (true or false)
Null (empty set)
Object (data and code)
Resource (database query and open file)
Array (collection of values grouped together).

Arrays

There are many array variations including ‘multidimensional arrays’ (lists of lists) and ‘associative arrays’ (which use strings as location keys).

Array functions include these options:

‘array_keys’ returns all keys
‘array_push’ adds a value to end
‘array_pop’ removes a value from end
‘array_merge’ combines two or more arrays together
‘array_reverse’ reverses element order
‘array_search’ returns key, if value exists
‘array_values’ returns all values
‘in_array’ returns Boolean true of false, if value is present
‘sort’ order
‘rsort’ reverse order.

Array creation sample -

$animals = array(‘bird’, ‘giraffe’, ‘elephant’);
$firstAnimal = $animals[0];
echo “My favorite animal is the $firstAnimal<br />”;

Arrays are zero-indexed so that the first value is represented by a zero. The code above would output:

“My favorite animal is the bird.”

Classes

A class is an object template, guideline or blueprint that describes its structures, properties, functions and methods.

The following PHP5 code creates a class:

<?php

class User{
}

?>

Objects

Objects simplify code by combining data and code through object-oriented programming (OOP).

Quantity calculation can be accomplished with the following code:

<?php

class Cost {
private $merchandise, $price, $quantity, $total;
public function __construct ($iMerchandise, $iPrice, $iQuantity) {

$this->merchandise = $iMerchandise;
$this->price = $iPrice;
$this->quantity = $iQuantity;
$this->calculate();
}

private function calculate() {
$this->price = number_format($this->price, 2);
$this->total = number_format(($this->price * $this->quantity), 2);
}

public function __toString() {
return “You ordered ($this->quantity) ‘$this->merchandise’ at $$this->price, for a total of: $$this->total.”;
}
}

$mycost = new Cost(“Bananas”, 15, 30);
echo $mycost . “<br />\n”;
?>

Methods

Methods are the ways, functions and actions that objects support.

The following PHP5 code retrieves a list of class methods:

<?php

class User{
public $student;
private $ranking;
public function __construct($student, $ranking){
$this->setStudent($student);
$this->setRanking($ranking);
}
public function setStudent($student){
$this->student = $student;
}
public function getStudent(){
return $this->student;
}
public function setRanking($ranking){
$this->ranking = $ranking;
}
public function getRanking(){
return $this->ranking;
}
}

$individual = new User(‘Bob Jones’, 1);

$class = get_class($individual);

echo “Class: $class <br />\n”;

echo “Methods:<br />\n”;
print_r(get_class_methods($class));

echo ‘Vars:<br />\n’;
print_r(get_class_vars($class));

?>

This code prints out the following:

Class: User

Methods:
Array ([0]=>_construct[1]=>setStudent[2]=>getStudent[3]=>setRanking[4]=>getRanking)

Vars:
Array ( [student]=>[ranking]=>)

The PHP5 programming language has improved OOP allowing for faster, easier and more complex Web page coding. PHP5 has a large number of qualities that make it an outstanding choice for creating robust, vibrant Web pages. Its updated features have assisted developers in managing classes, objects and methods more effectively and efficiently.

Tags: , , , , , , ,



Leave a Comment

You must be logged in to post a comment.