OOP vs Procedural programming
OOP code useability
Objects have:
Identity: name
Attributes: color, size, fullness (adjectives
Behaviors: fill, empty, clean (verbs)
One of challenges in OOP is determining whether something needs to be defined as a separate object.
Classes: general description of similar objects
Classes have Name or type
Attributes: properties, data
Behaviors: operation using methods.
Method is part of a class and can only access data within that class
class: fruit is a template for objects: Apple, Banana, Mango
<?php
class Fruit {
// Properties
public $name;
public $color;
// Methods
function set_name($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
}
$apple = new Fruit();
$banana = new Fruit();
$apple->set_name('Apple');
$banana->set_name('Banana');
echo $apple->get_name();
echo "<br>";
echo $banana->get_name();
?>
Many languages come with libraries of predefined classes for things like:
Strings Dates Collections File I/O
Networking etc.
The $this keyword refers to the current object, and is only available inside methods.
The __construct() function is automatically called when you create an object from the class.
<?php
class Fruit {
public $name;
public $color;
function __construct($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
}
$apple = new Fruit("Apple");
echo $apple->get_name();
?>
Similarly, __destruct(0 function is automatically called at the end of the script.
<?php
class Fruit {
public $name;
public $color;
function __construct($name) {
$this->name = $name;
}
function __destruct() {
echo "The fruit is {$this->name}.";
}
}
$apple = new Fruit("Apple");
?>
Access Modifiers
There are three access modifiers:
public
- the property or method can be accessed from everywhere. This is defaultprotected
- the property or method can be accessed within the class and by classes derived from that classprivate
- the property or method can ONLY be accessed within the class
Inheritance
in OOP the extends keyword. The child class will inherit all the public and protected properties and methods from the parent class. In addition, it can have its own properties and methods. Not the private.
final keyword. prevents inheritance and overriding.
?php
final class Fruit {
// some code
}
// will result in error
class Strawberry extends Fruit {
// some code
}
?>
A class constant
is declared inside a class with the const
keyword. Can be accessed outside the class by using the clas name followed by the scope resolution opertor(::) folowed by the constant name like here:
<?php
class Goodbye {
const LEAVING_MESSAGE = "Thank you for visiting W3Schools.com!";
}
echo Goodbye::LEAVING_MESSAGE;
?>
Or, we can access a constant from inside the class by using the self
keyword followed by the scope resolution operator (::
) followed by the constant name
Abstraction
Think of person. You have an idea of what a person is, not specific
Focus on the essential qualities of something.
The child class method must be defined with the same name, and the same or a less restricted access modifier.
see PHP OOP Abstract Classes (w3schools.com)
Interface
make it easy to use a variety of different classes in the same way. When one or more classes use the same interface, it is referred to as "polymorphism".
<?php
interface Animal {
public function makeSound();
}
class Cat implements Animal {
public function makeSound() {
echo "Meow";
}
}
$animal = new Cat();
$animal->makeSound();
?>
Interface are similar to abstract classes. The difference between interfaces and abstract classes are:
- Interfaces cannot have properties, while abstract classes can
- All interface methods must be public, while abstract class methods is public or protected
- All methods in an interface are abstract, so they cannot be implemented in code and the abstract keyword is not necessary
- Classes can implement an interface while inheriting from another class at the same time
Polymorphism:
different methods that use same inputs to generate the desired output
Static Polymorphism uses Method Overloading which
Implements multiple methods with the same name, but different parameters with results that are not the same.
Traits
PHP only supports single inheritance: a child class can inherit only from one single parent.
So, what if a class needs to inherit multiple behaviors? OOP traits solve this problem.
PHP OOP Traits (w3schools.com)
Static Methods
Static methods can be called directly - without creating an instance of the class first.
Static methods are declared with the static
keyword:
To access a static method use the class name, double colon(::), and the method name:
<?php
class greeting {
public static function welcome() {
echo "Hello World!";
}
}
// Call static method
greeting::welcome();
?>
Static Properties
Static properties can be called directly - without creating an instance of a class.
Static properties are declared with the static
keyword:
To access a static property use the class name, double colon (::), and the property name:
<?php
class pi {
public static $value = 3.14159;
}
// Get static property
echo pi::$value;
?>
<?php
class pi {
public static $value=3.14159;
}
class x extends pi {
public function xStatic() {
return parent::$value;
}
}
// Get value of static property directly via child class
echo x::$value;
// Get value of static property via xStatic() method
$x = new x();
echo $x->xStatic();
?>
Namespaces
Namespaces are qualifiers that solve two different problems:
- They allow for better organization by grouping classes that work together to perform a task
- They allow the same name to be used for more than one class
Iterables
PHP - Creating Iterables
Arrays
All arrays are iterables, so any array can be used as an argument of a function that requires an iterable.
Iterators (seems easier to use and array than create an Iterator???)
Any object that implements the Iterator
interface can be used as an argument of a function that requires an iterable.
An iterator contains a list of items and provides methods to loop through them. It keeps a pointer to one of the elements in the list. Each item in the list should have a key which can be used to find the item.
An iterator must have these methods:
current()
- Returns the element that the pointer is currently pointing to. It can be any data typekey()
Returns the key associated with the current element in the list. It can only be an integer, float, boolean or stringnext()
Moves the pointer to the next element in the listrewind()
Moves the pointer to the first element in the listvalid()
If the internal pointer is not pointing to any element (for example, if next() was called at the end of the list), this should return false. It returns true in any other case
Encapsulation bundle of elements of an object
Protecting elements of the class from inadvertent changes
Providing methods for external objects to properly operate on the class
Analysis: What do you need to do
Design: How you are going to do it.
1. Gather requirements.
2. Describe the application.
3. Identify the main objects
4. Describe the interactions
5. Create a class diagram.
Unified Modeling Language (UML) is not a programming language. Standardized notation for diagrams to visualize object-oriented systems.
- Details
- Written by: Bernie
- Category: PHP Resources
- Hits: 137