Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 12

PHP CLASSES AND OBJECTS

Object-Oriented Programming

object-oriented programming (OOP) is a method for decomposing the program into modules.

Object-oriented programming (OOP) is a programming paradigm that represents concepts as "objects" that
have data fields (attributes that describe the object) and associated functions known as methods. Objects, which
are usually instances of classes, are used to interact with one another to design applications and computer programs.
Object-Oriented Programming

 A language that is object oriented must provide support for three key language features:
1. Encapsulation and data abstraction
2. inheritance
3. Dynamic binding of method calls to methods
Concepts of Object‐Oriented Programming
(Abstraction)

An abstraction is a view or representation of an entity that includes only the most significant
attributes. In a general sense, abstraction allows one to collect instances of entities into groups in
which their common attributes need not be considered.

The purpose of abstraction is to simplify the programming process. It is a weapon against


complexity; a means of making large and/or complicated programs more manageable.
For example, suppose we define birds to be creatures with the following attributes: two wings, two legs, a tail,
and feathers.
Then, if we say a crow is a bird, a description of a crow need not include those attributes. The attributes that
distinguish crows are being black, being of a particular size, and being noisy.
Classes vs. Objects

 Classes are used to create user-defined data structures. Classes define functions called
methods, which identify the behaviors and actions that an object created from the class
can perform with its data. A class is a collection of objects similar types. The data
defined in a class definition are called data members of that class, and the functions
defined in a class definition are called member functions of that class.
 Instance of a class(Objects).Thus, an instance is an object that is built from a class and
contains real data.
Concepts of Object‐Oriented Programming
(Inheritance)

 Inheritance is the process by which objects of one class acquired the properties of objects
of another classes.
 It is a way of extending an existing type, not just by adding and modifying operations,
but also by adding data to the type.
Object Life Cycle

• Objects are created, used and discarded.

• We have special blocks of code (methods) that get called

- At the moment of creation (constructor)

- At the moment of destruction (destructor)

• Constructors are used a lot.

• Destructors are seldom used.


<?php
class Fruit {
// Properties
public $name;
public $color;

// Methods
function set_name($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
function set_color($color) {
$this->color = $color;
}
function get_color() {
return $this->color;
}}?>
Visibility

Class member variables also have scope.

 Public – can be accessed outside the class, inside the class, and in derived
classes

 Protected – can be accessed inside the class, and in derived classes


 Private – can only be accessed inside the class (i.e. private variables are not
visible in derived classes)
class MyClass
{
public $pub = 'Public';
protected $pro = 'Protected';
private $priv = 'Private';

function printHello()
{
echo $this->pub."\n";
echo $this->pro."\n";
echo $this->priv."\n";
}
}

$obj = new MyClass(); Public


echo $obj->pub."\n"; // Works Public
echo $obj->pro."\n"; // Fatal Error Protected
echo $obj->priv."\n"; // Fatal Error Private
$obj->printHello(); // Shows Public, Protected and Private
class MyClass2 extends MyClass
{

function printHello()
{
echo $this->pub."\n";
echo $this->pro."\n";
echo $this->priv."\n"; // Undefined
}
}
--- MyClass2 ---
echo("--- MyClass2 ---\n"); Public
$obj2 = new MyClass2(); Public
echo $obj2->pub."\n"; // Works Protected
(false)
$obj2->printHello(); // Shows Public, Protected, Undefined

You might also like