Midterms - Object Oriented Programming in PHP Flashcards
Defining a class
class keyword, class name, curly brace
<?php class Fruit { //Properties public $name; public $color; //Methods function set_name($name){ $this->name = $name; } function get_name(){ return $this->name; } } ?>
Objects of a class are created using the
new keyword
$apple = new Fruit ();
refers to the current object, and is only available inside methods.
$this keyword
<?php class Fruit { public $name; function set_name($name) { $this->name = $name; } } $apple = new Fruit(); $apple->set_name("Apple"); echo $apple->name; ?>
using $this keyword to change the value of a property
<?php class Fruit{ public $name; function set_name ($name){ $this->name = $name; } } $apple = new Fruit(); $apple->set_name("Apple"); echo $apple->name; ?>
ou can use the ____ keyword to check if an object belongs to a specific class:
instanceof
<?php $apple = new Fruit(); var_dump($apple instanceof Fruit);
allows you to initialize an object’s properties upon creation of the object
constructor
function_construct($name){ $this->name = $name; }
is called when the object is destructed or the script is stopped or exited
destruct Function
function_destruct(){ echo "The fruit is ($this->name)."; }
the property or method can be accessed from everywhere. This is default
public
the property or method can be accessed within the class and by classes derived from that class
protected
the property or method can ONLY be accessed within the class
private
When a class derives from another class.
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
Inheritance
An inherited class is defined by using the
extends keyword
class Strawberry extends Fruit { }
Inherited methods can be overridden by redefining the methods (use the same name) in the child class
Overriding
can be used to prevent class inheritance or to prevent method overriding.
final keyword
final class Fruit{ }
cannot be changed once it is declared
costant
Class constants are
A. case sensitive
B. case insensitive
A. case sensitive
how to access constant from outside the class?
class name, scope resolution operator (::), constant name
<?php class Goodbye{ const LEAVING_MESSAGE = "message"; } echo Goodbye::LEAVING_MESSAGE; ?>
how to access constant from inside the class?
self keyword, scope resolution operator (::), constant name
<?php class Goodbye{ const LEAVING_MESSAGE = "message"; public function byebye(){ echo self::LEAVING_MESSAGE; } } $goodbye = new Goodbye(); $goodbye->byebye(); ?>
are when the parent class has a named method, but need its child class(es) to fill out the tasks
Abstract Classes
is a method that is declared, but not implemented in the code.
abstract method
allow you to specify what methods a class should implement.
Interfaces
Interfaces are declared with the
interface keyword
<?php interface InterfaceName{ } ?>
____ cannot have properties but ____ can
Interfaces, Abstract classes
interface methods must be
public
abstract class methods must be
public or protected
When one or more classes use the same interface, it is referred to as
polymorphism
to implement an interface, a class must use the
implements keyword
<?php interface Animal{ public function makeSound() } class Cat implements Animal{ public function makesound(){ echo "Meow"; } } $animal = new Cat (); $animal->makesound(); ?>
A user-defined function declaration starts with the
function keyword
function myMessage(){ echo "Hello World"; }
Information can be passed to functions through
arguments
Arguments are specified after the function name, inside the
parentheses
function familyName($fname){ echo "$fname LastName.<br>"; } familyName ("John");
to let a function return a value use the
return statement
function sum ($x, y$){ $z = $x + $y; return $z; } echo "5+10 = " . sum(5,10) . "<br>";
To turn a function argument into a reference, use the
& operator
function add_five(&$value){ $value += 5; } $num = 2; add_five ($num); echo $num;
array function for counting arrays
count ()
$cars = array ("Volvo", "BMW", "Toyota"); echo count ($cars);
To access an array item you can refer to the
index number
$cars = array ("Volvo", "BMW", "Toyota"); echo $cars[0];
are arrays that use named keys that you assign to them
Associative arrays
$car = array (“brand” => “Ford”, “model” => “Mustang”, “year” => 1964);
echo $car [“model”];