Lecture 1 - OOP PHP
Lecture 1 - OOP PHP
Course Code: CSC 4182 Course Title: Advanced Programming In Web Technologies
• For running php the files should be kept in htdocs folder and requested through
server
PHP Syntax
• The PHP file should be saved with .php extension.
• PHP scripts starts with <?php and ends with ?>
• PHP file may contain HTML tags also with PHP scripts.
• The <?php ?> tag can be placed anywhere in PHP file.
• echo keyword is used to output in HTML.
• PHP statements ends with semicolon (;).
• In PHP keywords (if, else, while, echo, PHP etc.) ,classes, functions,
user-defined functions are not case sensitive.
• PHP variable names are case-sensitive.
• PHP comments
• <?php
//single line comment
#single line comment
/*
Multi
Line comment
*/
• ?>
• PHP is a loosely typed language so we need to declare data type of
variable.
• Variables are declared with dollar ($) in PHP.
• $$var is a reference variable that stores the value of the $variable inside
it.
Functions in PHP
• In programming functions are some block of codes that to be used multiple times.
• PHP has over 1000 built in functions which can be used directly.
• User defined function declared with function keyword.
• User defined functions in PHP can be defined like below
function myfunction(parameter_list){
Statements to be executed;
}
• A function name must starts with a letter or an underscore.
• To call a function we need to call it by its name with or without parameters
(arguments).
myfunction() //calling a function
• One function can have as many arguments as you want, just separate them with a
comma.
• As PHP is loosely typed language that’s why there is no return type in function
definition but value can be returned with return keyword.
Functions in PHP
Function without parameter
(.) dot is used to concatenate in
<?php php
function print_(){ Function with return
echo "Hello World " . "<br>"; <?php
} function add($a, $b){
print_(); calling a function $c = $a + $b;
PRINT_(); Also Correct return $c;
PriNt_(); Also Correct }
?> $result = add(10,12);
?>
function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
function __destruct() {
echo "The fruit is {$this->name} and the color is {$this->color}.";
}
}
$apple = new Fruit("Apple", "red");
?>
Getters and Setters
• Getters and setter are functions for accessing private and
protected properties.
<?php
class Fruit {
public $name;
public $color;
function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
function set_name($name){
$this->name = $name;
}
function set_color($color){
$this->color = $color;
}
function get_name(){
return $this->name;
}
function get_color(){
return $this->color;
}
function __destruct() {
echo "The fruit is {$this->name} and the color is {$this->color}.";
}
}
$apple = new Fruit("Apple", "red");
?>
Static Members
• A class can have static methods and properties which can be used without
creating object instance of a class.
• Static members are declared with static keyword.
• To access static members double colon (::) is used.
• className :: property;
• className::method();
• A class can have both static and non static members.
• A static member can be accessed from a method in the same class using the self
keyword and double colon (::)
• self::property;
• self::method();
• In terms of inheritance a static member of parent can be called using parent
keyword and double colon (::). In this case the access modifier must be public or
protected.
• parent::property;
• parent::method();
Static Members Examples
<?php Using parent in sub classes
class greeting {
<?php
public static $course = "ADV. WEB ";
class domain {
public static function welcome() {
protected static function getWebsiteName() {
echo "Hello World!";
return "W3Schools.com";
}
}
}
}
extends used to inherit
// Call static members
class domainW3 extends domain {
greeting::welcome();
public $websiteName;
echo greeting::$course;
public function __construct() {
?>
$this->websiteName = parent::getWebsiteName();
Using self in other functions }
<?php }
class greeting {
public static $course = "ADV. WEB "; $domainW3 = new domainW3;
public static function welcome() { echo $domainW3 -> websiteName;
echo "Hello World!"; ?>
}
public function __construct() {
self::welcome();
echo greeting::$course;
}
}
new greeting();
?>
Object Creation with and without ()
• As discussed earlier PHP objects can be created with new keyword.
• We can create object with or without () parenthesis but its better to follow
one.
• As we can not have overloaded functions in php we can not also overloaded
constructors also.
• If we have __construct() function with parameters then we need to pass
required parameters to create a object.
• If we want to pass a variable number of arguments to constructor (like
overloading) then we can use 3 dots(…) technique to pass variable number of
arguments.
• To access a class member in class body you must use $this->member. }
Object Examples
<?php
class Fruit {
public $name; //properties
public $color; Variable attributes
function __construct(...$attributes) {
//print_r($attributes);
$this->name = $attributes[0];
$this->color = $attributes[1];
}