CHAPTER 2 - OBJECT ORIENTED PROGRAMMING (OOP) Flashcards
OOP stands for
OBJECT PROGRAMMING PROGRAMMING
__________ ___________ __________ creating objects that contain both data and functions.
OOP
__________ ____________writing procedures or functions that perform operations on the data
PROCEDURAL PROGRAMMING
Advantages of OOP
- OOP is faster and easier to execute
- OOP provides a clear structure for the programs
- OOP helps to keep the PHP code DRY “Don’t Repeat Yourself”, and makes the code easier to maintain, modify and debug
- OOP makes it possible to create full reusable applications with less code and shorter development time.
“DRY” stands for
Don’t Repeat Yourself
It is a principle about reducing the repetition of code. You should extract out the codes that are common for the application, and place them at a single place and reuse them instead of repeating it.
DRY - don’t repeat yourself
two main aspects of object-oriented programming.
- classes
- Objects
_________ is a template for objects
CLASS
_______ Instance of a class
OBJECT
Define a class
<?php
class fruit{
//properties
//methods
}
define public properties (note: we can also use private and protected)
public $name;
public $age;
define set and get method with property name
(public $name;)
public function setname($n){
$this->name = $n;
}
public function getname(){
return $this->name;
}
instantiate the class Fruit:
$banana = new Fruit();
($banana is the instance/object of a class fruit)
set the name mango as the value of the instance $banana, and echo $banana.
$banana->setname(‘mango’);
echo $banana->getname();
What is “$this” keyword?
it refers to the current object and is only available inside methods.