3.php Classes and Objects
3.php Classes and Objects
3.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.
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
// 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
Public – can be accessed outside the class, inside the class, and in derived
classes
function printHello()
{
echo $this->pub."\n";
echo $this->pro."\n";
echo $this->priv."\n";
}
}
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