Dcit 201-Programming I: Session 4 - Introduction To PHP

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

DCIT 201- PROGRAMMING I

Session 4 – Introduction to PHP

Lecturer: Mr. Paul Ammah, CSD


Contact Information: [email protected]

Department of Computer Science


School of Physical and Mathematical Science
Session Objectives
• Understand class, objects and functions
• Be able to implement subclasses that inherit and override
superclass methods

Slide 2
Session Outline
The key topics to be covered in the session are as follows:
• Understand the concepts of classes, objects and encapsulation
using PHP
• Be able to implement instance variables, methods, and
constructors
• Understand and apply access modifiers
• Be able to design, implement and test self-written classes
• Understand the behavior of objects references, static variables,
and static methods

Slide 3
Topic One

CLASSES AND OBJECTS

Slide 4
Class and Object

class Product
{
// class body
}

$product = new Product();

var_dump($product);
Slide 5
Setting Properties in a Class
class Product
{
public $title = "default product";
public $producerMainName = "main name";
public $producerFirstName = "first name";
public $price = 0;
}

Slide 6
Accessing Object Properties
• Properties are accessed using the arrow notation
• Arrow notation also can be used to access
functions
$product = new Product();
print $product->title;

Slide 7
Methods
• The function keyword precedes a method name, followed
by an optional list of argument variables in parentheses.
• Like properties, methods can be declared public, protected,
or private
• A method can return a value or none.
• The method body is enclosed by braces:

public function myMethod($argument, $another)


{
// ...
}

Slide 8
Constructor Method
• A constructor method is invoked when an object is created.
• A constructor method in PHP has the following signature:

public function __construct($args) {


}

Slide 9
$this Operator
public function __construct(
$title, $firstName, $mainName, $price
) {
$this->title = $title;
$this->producerFirstName = $firstName;
$this->producerMainName = $mainName;
$this->price = $price;
}

• $this operator is used to access methods and properties within a


class

Slide 10
Primitive Types
• PHP is a loosely typed language.
• This means that there is no necessity for a variable to be
declared to hold a particular data type.
• This does not mean that PHP has no concept of type.
• Every value that can be assigned to a variable has a type.
• You can determine the type of a variable’s value using one
of PHP’s type-checking functions

Slide 11
Primitive Types cont’d

Slide 12
Type Declaration

Slide 13
Inheritance
• Inheritance is achieved using the extends keyword:
class CdProduct extends Product
{
//…..
}

Slide 14
Constructors and Inheritance
• When you define a constructor in a child class, you become
responsible for passing any arguments on to the parent.
• If you fail to do this, you can end up with a partially
constructed object.
• To refer to a method in the context of a class rather than an
object, you use :: rather than ->:
parent::__construct()

Slide 15
Invoking an Overridden Method
• The parent keyword can be used with any method that
overrides its counterpart in a parent class.
• When you override a method, you may not wish to
obliterate the functionality of the parent, but rather to
extend it.

Slide 16
Public, Private, and Protected
• Elements in your classes can be declared public,
private, or protected:
– A public properties and methods can be accessed from any
context.
– A private method or property can only be accessed from
within the enclosing class. Even subclasses have no access.
– A protected method or property can only be accessed from
within either the enclosing class or from a subclass. No
external code is granted access.

Slide 17
Static Methods and Properties
• You can access both methods and properties in the
context of a class rather than that of an object.
• Such methods and properties are “static” and must be
declared as such by using the static keyword
class StaticExample {
static public $aNum = 0;
public static function sayHello() {
print "hello";
}
}
print StaticExample::$aNum;
StaticExample::sayHello();

Slide 18

You might also like