Anda di halaman 1dari 16

Object Oriented in PHP

Why Object Oriented?

First PHP is not an object oriented


These underwent comprehensive revision and
enhancement when PHP 5 was released in July
2004

So what is OOP, and how does it fit into PHP?

Understanding Object-Oriented
Programming
Object-oriented programming is a style of
coding that allows developers to group similar
tasks into classes. This helps keep code
following the tenet dont repeat yourself
(DRY) and easy-to-maintain.

Understanding Objects and Classes


Before you can get too deep about OOP, a basic
understanding of the differences between
objects and classes is necessary.

Recognizing the Differences


Between Objects and Classes

A class, for example, is like a blueprint for a house


An object, then, is like the actual house
Classes form the structure of data and actions and
use that information to build objects.

Another Example Class and Object

Another Example Class and Object

Example: Structuring Classes


<?php
class MyClass
{
// Class properties and methods go here
}
?>
After creating the class, a new class can be
instantiated and stored in a variable using the new
keyword, like this:
$obj = new MyClass;
To see the contents of the class, use var_dump():
var_dump($obj);

Example: Structuring Classes


Try out this process by putting all the preceding code
in a new file called myclass.php in your localhost
testing folder:
<?php
class MyClass
{
// Class properties and methods
go here
}
$obj = new MyClass;

var_dump($obj);
?>

Defining Class Properties


To add data to a class, properties, or classspecific variables, are used. These work exactly
like regular variables, except theyre bound to
the object and therefore can only be accessed
using the object.
Example:
<?php
http://localhost/meeting12/myclass.php
class MyClass
{
public $prop1 = "I'm a class property!";
}
$obj = new MyClass;
var_dump($obj);
?>

Defining Class Properties contd


To read this property and output it to the
browser, reference the object from which to
read and the property to be read, like this:
echo $obj->prop1;
Example:
<?php
http://localhost/meeting12/myclass2.php
class MyClass
{
public $prop1 = "I'm a class property!";
}
$obj = new MyClass;
echo $obj->prop1;
?>

Defining Class Methods


Methods are class-specific functions. Individual
actions that an object will be able to perform
are defined within the class as methods.

Example:
http://localhost/meeting12/myclasswithmethod.php
<?php
class MyClass
{
public $prop1 = "I'm a class property!"; //property
public function setProperty($newval) //class set
{
$this->prop1 = $newval;
}
public function getProperty() //class get
{
return $this->prop1 . "<br />";
}

}
$obj = new MyClass;
echo $obj->prop1;
?>

Note OOP allows objects to reference themselves


using $this. When working within a method, use $this
in the same way you would use the object name
outside the class.

Defining Class Methods contd

To use these methods, read the property from


MyClass, change its value, and read it out again
by making the modifications below:
Example:
<?php
http://localhost/meeting12/myclasswithmethod2.php
class MyClass
{
// some code like before
}
$obj = new MyClass;
echo $obj->getProperty(); // Get the property value
$obj->setProperty("I'm a new property value!"); // Set a new one
echo $obj->getProperty(); // Read it out again to show the change
?>

Using Class Methods and Property


The power of OOP becomes apparent when
using multiple instances of the same class.
Example:
<?php
class MyClass
{
// some code like before
}
// Get the value of $prop1 from both objects
echo $obj->getProperty();
echo $obj2->getProperty();
// Set new values for both objects
$obj->setProperty("I'm a new property value!");
$obj2->setProperty("I belong to the second instance!");
// Output both objects' $prop1 value
echo $obj->getProperty();
echo $obj2->getProperty();
?>

Using Class Methods and Property


Result:

What do you think about this??

THANKS

Anda mungkin juga menyukai