Chapter 5 - PHP Fundamentals

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

Chapter 4

PHP Fundamentals

CSE@ASTU 1
Outline
• Overview of PHP
• Fundamentals of programming in PHP
• Introduction to OOP in PHP
• On the lab session

How to start working with PHP

Installation: XAMP/WAMP, Editor

Writing first PHP program

CSE@ASTU 2
PHP overview

• PHP is a recursive acronym which stands for ‘Hypertext Pre-processor


– Open source server side scripting language designed specifically for the web
– Server side scripting - Used to generate dynamic web pages
– Interpreted language, scripts are parsed at run time
– Embedded directly in to HTML
– Supports a wide-range of databases (20+ODBC)
– support for talking to other services using protocols

3
Why we learn PHP?

• Easy to learn, setup, host


• Multipurpose

Can be used to develop simple to complex website, CRM portal, E-commerce,
Image/video/audio/ processing, wide range DB support,
• High demand on job market
• PHP CMS’s support

Word press (CMS), Magneto(E-commerce), Drupal (CMS+E-commerce)
• PHP frameworks, Laravel, Symphony, CodeIgniter, yii

4
Overview ….

• Structurally similar to c/c++


• Supports procedural and object oriented paradigm
• Each PHP script must be enclosed in the reserved PHP tag

5
variables

• Declared as $var_name = value;


– names must be preceded by $ sign
– Are case sensitive

6
Constants

• Used to define values that cant change while the script is running,
such as configuration settings
• defined using defin() function which accepts two arguments
– the name of the constant and its value

7
Data types

• PHP supports total eight primitive data types:


– Integer $a = 123
– Floating point $a = 1.23
– String $a = 'Hello world!';
– Booleans $show_error = true;
– Array $colors = array("Red", "Green", "Blue");
– Object $message = new greeting;
– resource $link = mysql_connect("localhost", "root", "");
– NULL $a = NULL;
8
Conditional ….

9
Conditional statements

10
PHP Array

• Indexed array — An array with a numeric key.


• Associative array — An array where each key has its own specific value.
• Multidimensional array — An array containing one or more arrays within itself.

11
Loops

• while, do…while, for, foreach

2019 / 2011 EC 12
Functions

Reading assignment:
- callback functions, anonymous functions, arrow functions

13
Class and objects

• Classes and objects are the two main aspects of object-oriented


programming
• A class is a self-contained, independent collection of variables and functions
which work together to perform one or more specific tasks,
– while objects are individual instances of a class.
• A class is a template or blueprint from which individual objects can be
created

14
Class

Abebe Hadra

15
class

• A class can be declared using the class keyword, followed by the name of
the class and a pair of curly braces ({})
• Once a class has been defined, objects can be created from the class with
the new keyword.
• Class methods and properties can directly be accessed through pseudo-
variable $this object instance.
• The arrow symbol (->) is an OOP construct that is used to access contained
properties and methods of a given object
2019 / 2011 EC 16
Defining a class, properties and methods
<?php
//Rectangle.php
class Rectangle
{
// Declare properties
public $length = 0;
public $width = 0;

// Method to get the perimeter


public function getPerimeter(){
return (2 * ($this->length + $this->width));
}

// Method to get the area


public function getArea(){
return ($this->length * $this->width);
}
}
?>
17
Extending classes through inheritance

• Classes can inherit the properties and methods of another


class using the extends keyword.
• Sub-classes inherit the methods and properties of the super
class
– Sub-classes can also override their definitions

18
<?php class Customer extends User {
var $city;
class User { var $state; // or province
var $country;
var $is_admin = false;
function location() {
return $this->city . ", " . $this->state . ", " . $this->country;
var $first_name;
}
var $last_name;
var $username; }

function full_name() { class AdminUser extends User {


return $this->first_name . " " . $this->last_name; var $is_admin = true;
}
} function full_name() {
return $this->first_name . " " . $this->last_name . " (Admin)";
}
}
CSE@ASTU 19
Inheritance

20
Visibility modifiers

• Use to control access to properties and methods

21
Magic methods

• Methods that are called in some circumstances , eg.


 __construct() - executed automatically whenever a new
object is created
 __destruct() - executed automatically when the object is
destroyed
• Must be marked public

22
Static ….

23
Static properties and methods

• Behaviors related to the class generally


• Not tied to a particular instance
• Accessible directly from the class with out any instance
– accessible without needing an instantiation of the class
• Defined using the keyword static
• Static properties and methods can be accessed using the scope
resolution operator (::), like
– ClassName::$property and
24
Static….

• Static properties and methods are


inherited

• The keyword self in the example means


"the current class".
• It is never preceded by a dollar sign ($)
and
• always followed by the :: operator (e.g.
self::$name).

25
Inherited static ….

• Static properties and methods are inherited


• Visibility modifiers function in the same way
• Inherited static properties are shared variables
• Changes to the parent values changes the subclass values
• Changes to a subclass value change the parent value

26
Static….

27
Class constants

• used for class values that do not change


• Values may contain expression
• Defined by using the keyword const
• Support for visibility modifier since PHP 7.1
• Referenced with
– ClassName::constantName of self:: constantName

28
Contants….

29
Setter and getter methods

30
Setter & getter..

31
Setter & getter…

• Allow access to private properties


– Through public methods
• Useful to regulate access
• Useful for read-only and write-only properties
• Useful for pre-processing values
• Avoid “naïve setter” and “naïve getter” methods
– Like the example in the previous slide

32
33
Next session Topics

• Forms
• Session and cookies
• Files basics
• Database
 CRUD

2019 / 2011 EC 34
Reading Assignment

• PHP strings
• Operators
• Late static binding
• Super global
• Autoloading

2019 / 2011 EC 35

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy