oop php
play

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


  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 ¡

  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 ¡

  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 ¡

  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 ¡

  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 ¡

  6. Other ¡funcKons ¡ • Ex: ¡serialize(), ¡unserialize() ¡ – Allow ¡serializaKon ¡and ¡deserializaKon ¡of ¡PHP ¡ objects ¡ • 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 ¡

  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 ¡

  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 ¡ 8 ¡ Lecture ¡6 ¡

  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) ¡ 9 ¡ Lecture ¡6 ¡

  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) ¡ 10 ¡ Lecture ¡6 ¡

  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 ¡ 11 ¡ Lecture ¡6 ¡

  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 ¡ 12 ¡ Lecture ¡6 ¡

  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 13 ¡ Lecture ¡6 ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend