PHP - Object oriented code - basics
We will be more oriented on objects in PHP5 than PHP4, because there are major changes in object model and more features in PHP5. If you want to use objects in PHP, I would recommend you to switch to PHP5.
In object oriented programming, you have something called class and something called object. A class could be described as a blueprint for an object, description of an object, a template which is used to build an object. When we build an object from a class, we say we have created object of that class. For example, if we use class Animal to build object Cat, then object Cat is of Animal class.
PHP Manual presents object as a set of variables and functions which operates with those variables. This is good description.
Objects are independent portions of code. We use objects to group similar functions (eg functions for managing Persons info, functions to access particular table in database, etc…). If they are designed well, they can be stripped out from one application and transfered to another one (they are reusable). Also, when you use objects, you are not interested in how objects do their inside work. You just see object's interface (class functions and variables you have access to) and use it. You do not see the inside operations.
Another important mechanism in object oriented programming is Design patterns. Design patterns is a set of rules which describes best practices in class design. If you design your classes design patterns, you are ensuring scalability and easy (easier) maintenance of your code. Design patterns is an advanced topic but I will present you with several most commonly used design patterns in later examples.
In this example we will see basic class declaration and object creation. These are same in both PHP4 and PHP5. Some of our examples are classes (eg Storing files into mysql table).
We have a class Person describing properties of any person - its name, address and phone number. This class has three variables which hold these properties, and functions to change these variables. Every variable has get and set function. Class also has a function which displays persons info.
First, we declare a class. Then we make an object of class Person named Heidi. We make an object using keyword new. Remember the syntax. Variable = new ClassName;
$Heidi = new Person();
Then we set properties for Heidi. Then we make an object of class Person named Bred, and set properties for Bred. At the end, we print their info.
Required steps in using objects are: declare a class, create an object of that class, use object. You can not use a class itself. So, if you have a class Person, you can not call
Person->setName();
Remember, class is just a blueprint.
class Person {
private $name;
private $address;
private $phone;
function setName($name)
{
$this->name = $name;
}
function getName()
{
return $this->name;
}
function setAddress($address)
{
$this->address = $address;
}
function getAddress()
{
return $this->address;
}
function setPhone($phone)
{
$this->phone = $phone;
}
function getPhone()
{
return $this->phone;
}
function printPersonInfo()
{
echo $this->name . "\n";
echo $this->address . "\n";
echo $this->phone . "\n";
}
};
$Heidi = new Person();
$Heidi->setName("Heidi Clum");
$Heidi->setAddress("Somewhere Boulevard 1234");
$Heidi->setPhone("123456789");
$Bred = new Person();
$Bred->setName("Bred Bit");
$Bred->setAddress("Sunset Boulevard 4321");
$Bred->setPhone("987654321");
$Heidi->printPersonInfo();
$Bred->printPersonInfo();
?>
Related Marakana Courses
- PHP and MySQL Bootcamp Training
February 21st, 2007 at 2:33 pm
Could anybody recommend me an alternative environment for developing PHP MySQL Applications? I use PHPEclipse with Eclipse 3.2.1
February 21st, 2007 at 2:44 pm
I personally use a plain old text editor for all my work along with default Apache, mod PHP, and MySQL installation. I find FireFox very useful, especially with the FireBug plugin.
On Mac, I use vi and TextWrangler, and Crimson Editor on Windows (I try to do most development on OSX but some clients prefer Win still).
Hope this helps!
Marko
April 12th, 2007 at 12:59 pm
[...] The -> is for when referring to Object Oriented Programming. PHP - Object oriented code - basics | Marakana Blog [...]
March 17th, 2009 at 11:50 pm
"Could anybody recommend me an alternative environment for developing PHP MySQL Applications? I use PHPEclipse with Eclipse 3.2.1"
—- zend development environment.
April 8th, 2009 at 9:10 am
What does the "private" tag mean, why do you use it? Can that be any word or is private a built in PHP term?
ex
private $name;
private $address;
private $phone;
Thanks for the article, its really help understand some things.