1 / 42

Object Oriented Programming in PHP

Object Oriented Programming in PHP. Topics. Quick OOP Review Classes Magic Methods Static Methods Inheritance Exceptions Interfaces Operators Type Hinting Using PDO. What is an Object?. Encapsulated data with logic An object has properties (data) and methods (logic).

tevin
Download Presentation

Object Oriented Programming in PHP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object Oriented Programming in PHP

  2. Topics • Quick OOP Review • Classes • Magic Methods • Static Methods • Inheritance • Exceptions • Interfaces • Operators • Type Hinting • Using PDO

  3. What is an Object? Encapsulateddata with logic An object has properties (data) and methods (logic)

  4. Not everything is an object in PHP But all objects have (a) class

  5. What is a Class? A way of saying “all objects of this type share these attributes”

  6. Our world has many objects (chairs, exams, students, etc.) but we can recognize tables because: All tables have legs and a surface

  7. There are many kinds of objects in our code but the computer can recognize Users because… All Users have a username, a password and can authenticate()

  8. Class Syntax • Class Example { • public $foo; • const $baz = 42; • public function bar() { • // put code here • } • }

  9. Understanding Visibility • public – visible to everyone • protected – visible only to classes which are derived from this class • private – visible only to this class • default (nothing listed) – means public; only for methods

  10. Magic Methods • PHP will call some specially named methods to enable a class to perform specific operations. • They all start with __ • Never let any of your code start with __

  11. __construct() • Constructor • Called when a new instance is created

  12. __toString() • How to convert the object to a string • Used whenever you use your object with the print or . (concatenate) operators.

  13. Class Constants • Constants cannot change • They exist at the CLASS level not in instances. • Class Foo { const bar = 42; } • Print Foo::bar;

  14. Static Properties and Methods • Static members are CLASS level not instance level • class Foo { • public static $bar = “bar”; • public static function baz() {} • }

  15. self • Used to access static members of the current class (uses the most derived class) • self::$bar

  16. New Operators • new • -> • ::

  17. new operator • Used to instantiate a class. • $foo = new Foo();

  18. -> operator • Used to access a property or method of an instance. • $foo->bar(); • $foo->baz = 2;

  19. :: operator • Like the -> but works on the level of CLASSES not instances. • How you access static and constant members • Class Foo { const bar = 42; } • Print Foo::bar;

  20. Inheritance • A way of sharing commonbehaviour among classes • PHP is a single inheritance language • A class may have no parent or 1 parent only.

  21. Inheritance Syntax • class Bar {} • class Foo extends Bar {}

  22. Accessing the parent • class Foo extends Bar { • public function __construct() { • parent::__construct(); • parent::$foo; • } • }

  23. An abstract class is a class you cannot instantiate abstract class

  24. Correct • abstract class Foo { • public $foo; • }

  25. Correct • abstract class Foo { • abstract public bar(); • }

  26. Incorrect • class Foo { • abstract public bar(); • }

  27. Interfaces • An interface is like an abstract class where all the methods are abstract. An interface may not have any properties. • However, a class may IMPLEMENT multiple interfaces. • May not have private members.

  28. Interface Syntax • interface Fooable { • public function foo(); • protected function bar(); • } • class Foo implements Fooable { • public function foo() {} • protected function bar() [}; • }

  29. Passing Arguments by Reference or Copy

  30. Passing By Copy • When a scalar value is passed to a function it is passed “by copy”. • Changes to the value inside the function do not affect the value “outside”, or after, the function or call.

  31. Passing by Reference • When an object is passed to a function it is passed “by reference”. • Changes to the value inside the function do affect the value “outside”, or after, the function or call.

  32. Passed by Reference vs. Copy

  33. Type hinting lets us specify the type of a parameter. Type Hinting

  34. Hintable Types

  35. New Key Words and Operator • $this • self:: • parent:: • -> • :: • new • static • class • interface • public • private • protected

  36. PDO PHP Data Objects

  37. PDO • A common API for doing database operations with any database. • Supports most databases with the same classes/functions

  38. Create a Connection $dbh = new PDO( 'mysql:host=localhost;dbname=test', $user, $pass);

  39. Create a Statement $stmt = $dbh->prepare( "INSERT INTO table(col1, col2) VALUES (?, ?)");

  40. Execute the statement • $stmt->execute(array(1, 2)) • $stmt->execute()

  41. Fetch Results • while ($row = $stmt->fetch()) { • print_r($row); • } • foreach ( $stmt->fetchAll() as $row) { print_r($row) }

  42. PDO lifecycle

More Related