OOP PHP PHP is an object-oriented language See: - - PowerPoint PPT Presentation

oop php
SMART_READER_LITE
LIVE PREVIEW

OOP PHP PHP is an object-oriented language See: - - PowerPoint PPT Presentation

OOP PHP PHP is an object-oriented language See: h8p://us.php.net/manual/en/language.oop5.php Has classes + objects Has inheritance and method


slide-1
SLIDE 1

OOP ¡PHP ¡

PHP ¡is ¡an ¡object-­‑oriented ¡language ¡

  • See: ¡h8p://us.php.net/manual/en/language.oop5.php ¡ ¡ ¡

– Has ¡classes ¡+ ¡objects ¡ – Has ¡inheritance ¡and ¡method ¡overriding ¡

  • However, ¡the ¡dynamic ¡typing ¡of ¡PHP ¡variables ¡does ¡

not ¡give ¡it ¡quite ¡the ¡same ¡type ¡of ¡polymorphism ¡as ¡ Java ¡

– The ¡reference ¡type ¡always ¡matches ¡the ¡object ¡type ¡

– Object ¡syntax ¡is ¡more ¡like ¡C++ ¡than ¡Java ¡

  • Uses ¡the ¡scope ¡resoluKon ¡operator ¡for ¡parent ¡class ¡

access ¡-­‑ ¡:: ¡

  • Uses ¡the ¡“arrow” ¡operator ¡for ¡field ¡/ ¡method ¡access ¡

1 ¡

Lecture ¡6 ¡

slide-2
SLIDE 2

OOP ¡PHP ¡

PHP ¡objects ¡can ¡have ¡instance ¡variables ¡and ¡instance ¡ funcKons/methods ¡

  • Like ¡Java ¡(more ¡or ¡less) ¡we ¡can ¡restrict ¡visibility ¡by ¡using ¡

– private ¡ » Only ¡visible ¡within ¡class ¡of ¡variable’s ¡declaraKon ¡ – protected ¡ » Visible ¡within ¡class ¡of ¡variable’s ¡declaraKon, ¡plus ¡any ¡ subclasses ¡ – public ¡ » Visible ¡anywhere ¡

  • Unlike ¡Java ¡we ¡do ¡not ¡have ¡implicit ¡access ¡to ¡instance ¡

variables ¡from ¡within ¡objects ¡

– To ¡access ¡we ¡must ¡use ¡“this” ¡for ¡explicit ¡access ¡

2 ¡

Lecture ¡6 ¡

slide-3
SLIDE 3

OOP ¡PHP ¡

class Foo { private $x; public function setX($data) { $this->x = $data; } public function getX() { return $this->x; } ... }

  • See ¡what ¡happens ¡if ¡you ¡just ¡use ¡$x ¡

3 ¡

Lecture ¡6 ¡

slide-4
SLIDE 4

OOP ¡PHP ¡

  • PHP ¡also ¡has ¡a ¡lot ¡of ¡funcKons ¡to ¡help ¡with ¡

OOP ¡

– Some ¡are ¡parKcularly ¡useful ¡for ¡the ¡Web ¡ environment ¡in ¡which ¡PHP ¡is ¡used ¡ – Ex: ¡__autoload() ¡

  • Can ¡automaKcally ¡include ¡class ¡files ¡for ¡any ¡classes ¡

used ¡in ¡a ¡PHP ¡script ¡

– We ¡don’t ¡have ¡to ¡explicitly ¡include ¡each ¡file ¡ – We ¡don’t ¡have ¡to ¡worry ¡about ¡including ¡a ¡file ¡mulKple ¡Kmes ¡

  • Note ¡the ¡name: ¡prefixed ¡with ¡two ¡underscores ¡

– There ¡are ¡several ¡useful ¡funcKons ¡with ¡this ¡notaKon ¡ – Ex: ¡__construct(), ¡__destruct(), ¡__toString(), ¡__sleep(), ¡etc ¡

4 ¡

Lecture ¡6 ¡

slide-5
SLIDE 5

OOP ¡PHP ¡

  • These ¡are ¡called ¡“magic ¡methods” ¡

– Mostly ¡because ¡they ¡are ¡called ¡implicitly ¡in ¡some ¡ way ¡or ¡another ¡ – PHP ¡programmer ¡may ¡define ¡the ¡method ¡bodies ¡ but ¡does ¡not ¡explicitly ¡call ¡them ¡ – For ¡more ¡informaKon ¡see: ¡

  • h8p://php.net/manual/en/language.oop5.magic.php ¡ ¡

– See ¡ex14.php, ¡Foo.php ¡and ¡SubFoo.php ¡

5 ¡

Lecture ¡6 ¡

slide-6
SLIDE 6

Other ¡funcKons ¡

  • Ex: ¡serialize(), ¡unserialize() ¡

– Allow ¡serializaKon ¡and ¡deserializaKon ¡of ¡PHP ¡

  • bjects ¡
  • Generates ¡a ¡storable ¡representaKon ¡of ¡a ¡value ¡

– Values ¡do ¡not ¡lose ¡their ¡type ¡or ¡structure ¡

  • This ¡is ¡good ¡if ¡we ¡want ¡to ¡save ¡an ¡object ¡into ¡a ¡file ¡or ¡a ¡

cookie ¡and ¡then ¡later ¡restore ¡it ¡-­‑ ¡unserialize ¡

  • See ¡usesession-­‑oop.php ¡and ¡User.php ¡

Lecture ¡6 ¡ 6 ¡

slide-7
SLIDE 7

OOP ¡PHP ¡

– PHP ¡OOP ¡definitely ¡has ¡differences ¡from ¡Java ¡OOP ¡

  • However, ¡there ¡is ¡extensive ¡documentaKon ¡on ¡it ¡so ¡

avail ¡yourselves ¡of ¡it ¡

– Ex: ¡Interfaces ¡and ¡Polymorphism ¡

  • Since ¡PHP ¡variables ¡are ¡dynamically ¡typed, ¡we ¡never ¡

have ¡to ¡cast ¡objects ¡to ¡store ¡them ¡

  • See ¡ex15.php ¡and ¡class ¡files ¡

– Why ¡use ¡it ¡(or ¡when ¡to ¡use ¡it)? ¡

  • When ¡scripts ¡get ¡larger ¡/ ¡more ¡complex ¡
  • To ¡interact ¡with ¡some ¡predefined ¡resources ¡

– Ex: ¡a ¡MySQL ¡database ¡

7 ¡

Lecture ¡6 ¡

slide-8
SLIDE 8

8 ¡

Pa8erns ¡through ¡Regular ¡Expressions ¡

  • What ¡are ¡regular ¡expressions? ¡

– Expressions ¡that ¡can ¡be ¡generated ¡by ¡regular ¡ languages, ¡or ¡that ¡can ¡be ¡produced ¡by ¡a ¡finite ¡ automaton ¡ ¡

  • But ¡this ¡is ¡not ¡a ¡Theory ¡course ¡– ¡PHP ¡regular ¡

expressions ¡don't ¡exactly ¡match ¡formal ¡def. ¡

– PracKcally ¡speaking, ¡they ¡are ¡pa8erns ¡that ¡you ¡ can ¡use ¡to ¡match ¡various ¡parts ¡of ¡strings ¡

  • Allow ¡matches ¡to ¡be ¡made ¡when ¡the ¡exact ¡values ¡

to ¡be ¡matched ¡are ¡uncertain ¡

– one ¡le8er ¡followed ¡by ¡one ¡or ¡more ¡le8ers ¡or ¡digits ¡ – upper ¡or ¡lower ¡case ¡le8ers ¡

Lecture ¡6 ¡

slide-9
SLIDE 9

9 ¡

Regular ¡Expressions ¡

  • Regular ¡expressions ¡in ¡PHP ¡

– Typically ¡used ¡to ¡match ¡or ¡(match ¡and ¡subsKtute) ¡ strings ¡ – Useful ¡for ¡finding ¡files, ¡finding ¡strings ¡within ¡files, ¡ finding ¡substrings ¡within ¡strings, ¡finding ¡pa8erns ¡ within ¡strings, ¡global ¡subsKtuKons, ¡etc ¡

  • Syntax: ¡

– PHP ¡can ¡use ¡two ¡variaKons ¡of ¡regular ¡expressions ¡

  • POSIX ¡regular ¡expressions ¡

– Portable ¡OperaKng ¡System ¡Interface ¡(IEE ¡1003.1) ¡

Lecture ¡6 ¡

slide-10
SLIDE 10

10 ¡

Regular ¡Expressions ¡

– These ¡are ¡compiled ¡into ¡PHP ¡by ¡default ¡ – A ¡bit ¡simpler ¡to ¡use ¡ – Not ¡binary ¡safe ¡

  • What ¡does ¡this ¡mean? ¡See ¡link ¡

– h8p://www.php.net/regex ¡ ¡

  • PCRE ¡(Perl ¡CompaKble ¡Regular ¡Expressions) ¡

– Also ¡compiled ¡into ¡PHP ¡by ¡default ¡ – More ¡commonly ¡used ¡(i.e., ¡in ¡other ¡languages) ¡ – Support ¡a ¡few ¡addiKonal ¡features ¡

  • Ex: ¡Non-­‑greedy ¡matching ¡opKon ¡

– h8p://www.php.net/pcre ¡ ¡ – We ¡will ¡use ¡these, ¡since ¡they ¡are ¡also ¡used ¡in ¡Perl, ¡Java ¡ and ¡Javascript ¡(more ¡or ¡less) ¡

Lecture ¡6 ¡

slide-11
SLIDE 11

11 ¡

Regular ¡Expressions ¡

  • Let's ¡start ¡with ¡some ¡basics: ¡

– preg_match() ¡is ¡a ¡PHP ¡funcKon ¡to ¡match ¡regular ¡ expressions ¡

preg_match(pattern, subject, matches, flags, offset)

  • pa8ern ¡is ¡a ¡string ¡represenKng ¡legal ¡Perl-­‑type ¡regular ¡

expression ¡

– Delimited ¡by ¡/ ¡/ ¡(forward ¡slashes) ¡ – Much ¡more ¡to ¡discuss ¡here ¡– ¡soon ¡ ¡

  • subject ¡is ¡the ¡string ¡we ¡are ¡searching ¡within ¡

– If ¡we ¡include ¡only ¡the ¡pa8ern ¡and ¡subject, ¡it ¡amounts ¡to ¡a ¡ boolean ¡funcKon ¡telling ¡us ¡if ¡a ¡match ¡exists ¡or ¡not ¡

Lecture ¡6 ¡

slide-12
SLIDE 12

12 ¡

Regular ¡expressions ¡

  • The ¡other ¡parameters ¡allow ¡us ¡to ¡obtain ¡more ¡

informaKon ¡about ¡the ¡match ¡

– matches ¡is ¡an ¡array ¡

  • $matches[0] ¡returns ¡that ¡string ¡that ¡matched ¡the ¡pa8ern ¡
  • successive ¡indices ¡return ¡parenthesized ¡subpa8erns ¡-­‑-­‑ ¡later ¡ ¡

– flags ¡so ¡far ¡is ¡just ¡one ¡flag ¡

  • PREG_OFFSET_CAPTURE ¡
  • Allows ¡us ¡to ¡see ¡the ¡index ¡where ¡the ¡match ¡starts ¡

– offset ¡indicates ¡where ¡(index ¡in ¡string) ¡to ¡start ¡the ¡search ¡

– preg_match_all ¡allows ¡mulKple ¡matches ¡in ¡the ¡same ¡ string ¡

– Also ¡allows ¡addiKonal ¡flags ¡

– Let's ¡look ¡at ¡a ¡couple ¡very ¡simple ¡examples ¡

  • See ¡ex17.php ¡

Lecture ¡6 ¡

slide-13
SLIDE 13

13 ¡

Regular ¡Expressions ¡

  • Matching ¡variaKons ¡

– We ¡can ¡match ¡single ¡and ¡mulKple ¡character ¡ pa8erns, ¡and ¡combinaKons ¡of ¡the ¡two ¡

  • [ ¡] ¡indicates ¡a ¡"character ¡class", ¡or ¡that ¡exactly ¡one ¡of ¡

the ¡characters ¡indicated ¡must ¡match ¡the ¡target ¡

  • Can ¡include ¡single ¡characters, ¡sequences ¡and ¡

predefined ¡"character ¡classes" ¡

/./ # match any character (except newline) /[aeiouAEIOU]/ # match any vowel /[0-9+\-*\/]/ # match a digit or arithmetic operator /[^a-zA-Z0-9]/ # match any NON letter or digit /\w/ # match any "word" character (letter, digit # or underscore) /\s/ # match white space

Lecture ¡6 ¡

slide-14
SLIDE 14

14 ¡

Regular ¡Expressions ¡

  • Matching ¡sequences ¡of ¡characters ¡

– Perl ¡regular ¡expr. ¡provide ¡many ¡operators ¡to ¡ allow ¡for ¡various ¡pa8ern ¡groupings ¡

* : 0 or more of preceding character (or class) + : 1 or more of preceding character (or class) ? : 0 or 1 of preceding character (or class) {a,b} : between a and b matches of preceding character, where a and b are integers and a <= b.

  • $subject = "ae12abc r2d2 a1steaksauce";

$pattern = "/[aeiou]+[1234567890]*[a-z]?/"; if (preg_match_all($pattern, $subject, $result)): echo "$pattern was found: \n"; print_r($result); echo "<br />"; endif;

  • /[aeiou]+[1234567890]*[a-z]?/ was found: Array ( [0] => Array ( [0] =>

ae12a [1] => a1s [2] => eak [3] => auc [4] => e ) )

Lecture ¡6 ¡

slide-15
SLIDE 15

15 ¡

Regular ¡Expressions ¡

– By ¡default: ¡

  • Matches ¡can ¡be ¡made ¡ANYWHERE ¡in ¡a ¡string, ¡proceeding ¡

from ¡lev ¡to ¡right ¡

– We ¡can ¡ANCHOR ¡a ¡match ¡to ¡make ¡it ¡start ¡or ¡end ¡in ¡a ¡specific ¡ place ¡ \b ¡-­‑ ¡requires ¡a ¡word ¡boundary ¡(technically ¡it ¡is ¡an ¡asserKon ¡ rather ¡than ¡an ¡anchor) ¡ ^ ¡-­‑ ¡at ¡beginning ¡means ¡match ¡starts ¡there ¡ $ ¡-­‑ ¡"matches" ¡end ¡of ¡string ¡ – Consider ¡matching ¡a ¡legal ¡Java ¡idenKfier: ¡ /[a-zA-Z$_]+[\w$]*/ – One ¡or ¡more ¡le8ers ¡or ¡$ ¡or ¡_ ¡followed ¡by ¡0 ¡or ¡more ¡ "word" ¡characters ¡or ¡$ ¡ – However, ¡this ¡will ¡match ¡ANYWHERE ¡in ¡the ¡string ¡(i.e. ¡the ¡ match ¡could ¡be ¡a ¡substring) ¡ – If ¡we ¡want ¡the ¡enKre ¡string ¡to ¡be ¡an ¡idenKfier, ¡we ¡must ¡ make ¡sure ¡that ¡there ¡are ¡no ¡extraneous ¡characters ¡ – Let's ¡look ¡at ¡two ¡variaKons: ¡

Lecture ¡6 ¡

slide-16
SLIDE 16

16 ¡

Regular ¡Expressions ¡

/^[a-zA-Z$_]+[\w$]*/ /^\s*[a-zA-Z$_]+[\w$]*\s*$/

– First ¡opKon ¡requires ¡match ¡to ¡start ¡at ¡beginning ¡of ¡the ¡string ¡ » Does ¡nothing ¡about ¡the ¡end ¡ – Second ¡opKon ¡allows ¡for ¡leading ¡and ¡trailing ¡white ¡space, ¡and ¡ requires ¡end ¡of ¡match ¡to ¡be ¡end ¡of ¡string ¡ » Note ¡that ¡the ¡‘$’ ¡character ¡means ¡something ¡different ¡ within ¡the ¡character ¡class ¡than ¡it ¡does ¡outside ¡it! ¡

  • See ¡ex17.php ¡for ¡code ¡tesKng ¡these ¡

Lecture ¡6 ¡

slide-17
SLIDE 17

In ¡Class ¡Exercise ¡3 ¡

  • Install ¡the ¡PHPMailer ¡class ¡onto ¡your ¡Webserver ¡so ¡

that ¡you ¡can ¡send ¡mail ¡from ¡your ¡server ¡uKlizing ¡ ¡gmail ¡ The ¡PHPMailer ¡files ¡can ¡be ¡found ¡at: ¡ h8ps://github.com/Synchro/PHPMailer ¡ ¡

  • You ¡may ¡have ¡to ¡change ¡some ¡se{ngs ¡on ¡your ¡server ¡

and ¡update ¡your ¡include ¡path ¡in ¡order ¡to ¡uKlize ¡these ¡

  • files. ¡See ¡the ¡mail.php ¡and ¡sendmail.php ¡handouts ¡

from ¡lecture ¡for ¡help. ¡ ¡

  • Demonstrate ¡your ¡site ¡by ¡using ¡the ¡mail.php ¡and ¡

sendmail.php ¡scripts ¡to ¡send ¡yourself ¡an ¡email ¡in ¡front ¡

  • f ¡your ¡instructor. ¡You ¡must ¡have ¡some ¡mail ¡client ¡
  • pen ¡(either ¡on ¡a ¡computer ¡or ¡phone ¡or ¡tablet) ¡to ¡

show ¡that ¡the ¡mail ¡has ¡been ¡delivered. ¡ ¡

Lecture ¡6 ¡ 17 ¡