Word | Description |
---|---|
Object-Oriented | With Object-Oriented Programming, we can model the general idea by using a class. |
Class | A class is a unit of code that describes the characteristics and behaviors of something. |
Object | An object is a specific instance of a class. |
Properties | The characteristics of a class or object are known as its properties. |
Method | The behaviors(actions) of a class are known as its methods. |
In PHP we use class keyword to define a class.
The class definition has the following syntax.
class Car {
//properties
//methods
}
Here is the PHP code necessary to define a very basic Dog class:
<?PHP class Dog { public function bark() { print "PHP!"; } } ?>
Here the Dog class has just one method, bark().
A class is defining or creating a new type. We can create variables for the new type. We call it object in object oriented programming.
We can create an object by using the following syntax:
$aRectangleObject = new RectangleClass;
We need to use the special -> operator to reference the method.
<?PHP class Shape { public function say() { print "shape"; } } $aRectangle = new Shape; $aRectangle->say(); ?>
The code above generates the following result.
You can use objects inside other objects.
Using ->
to access objects within objects.
For example, we could define a NameTag
class and give
each Book
a NameTag
object like
this:
<?PHP class NameTag { public $Words; } class Book { public $Name; public $NameTag; public function say() { print "Book"; } } $aBook = new Book; $aBook->Name = "PHP"; $aBook->NameTag = new NameTag; $aBook->NameTag->Words = "from java2s.com"; ?>
The $NameTag
property is declared like any other, but needs to be created with new
once &aBook has been created.
Class properties are very similar to variables.
An object's property can store a single value, an array of values, or even another object.
To add a property to a class, first write the keyword public, private, or protected, followed by the property's name preceded by a $symbol.
public, private, or protected are the visibility levels you want to give to the property:
class MyClass { public $property1; // This is a public property private $property2; // This is a private property protected $property3; // This is a protected property }
We can initialize properties at the time that we declare them:
class MyClass { public $value = 123; }
We can access the corresponding object's property value from within your calling code by using the following syntax:
$object->property;
Write the name of the variable storing the object, followed by an arrow symbol composed of a hyphen (-) and a greater - than symbol (>), followed by the property name.
Note that the property name doesn't have a $ symbol before it.
We can add properties to a class.
<?PHP class Shape{ public $Name; public function say() { print "shape"; } } $aRectangle = new Shape; $aRectangle->say(); $aRectangle->Name = "Rect"; $aRectangle->say(); ?>
The code above generates the following result.
->
is used to work with the property, and also that there is
no dollar sign before Name.
Each object has its own set of properties. Consider the following code:
<?PHP class Shape{ public $Name; public function say() { print "shape"; } } class Rectangle extends Shape{ } $aRect = new Rectangle; $bRect = new Rectangle; $aRect->Name = "A"; $bRect->Name = "B"; print $aRect->Name; print $bRect->Name; ?>
PHP allows you to dynamically declare new properties for objects. It would create the property only for the object, and not for any other instances of the same class.
We can treat an object as an array with the foreach loop. foreach will iterate over each of the properties that are accessible.
That is, private and protected properties will not be accessible in the general scope.
Take a look at this script:
<?PHP class Person { public $FirstName = "James"; public $MiddleName = "Tuple"; public $LastName = "List"; private $Password = "pass"; public $Age = 29; public $HomeTown = "LA"; } $bill = new Person(); foreach($bill as $var => $value) { echo "$var is $value\n"; } ?>
The code above generates the following result.
Note that the $Password
property is nowhere in sight, because it is marked Private.
If the foreach
loop is called inside a method, we should be able to see the
property:
<?PHP class Person { public $FirstName = "James"; public $MiddleName = "Tuple"; public $LastName = "List"; private $Password = "pass"; public $Age = 29; public $HomeTown = "LA"; public function outputVars() { foreach($this as $var => $value) { echo "$var is $value\n"; } } } $bill = new Person(); $bill->outputVars(); ?>
The code above generates the following result.