Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Programming in PHP. Topics Quick OOP Review Classes Magic Methods Static Methods Inheritance Exceptions Interfaces Operators Type Hinting.

Similar presentations


Presentation on theme: "Object Oriented Programming in PHP. Topics Quick OOP Review Classes Magic Methods Static Methods Inheritance Exceptions Interfaces Operators Type Hinting."— 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? Encapsulated data 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 All tables have legs and a surface Our world has many objects (chairs, exams, students, etc.) but we can recognize tables because:

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

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 common behaviour 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 abstract class An abstract class is a class you cannot instantiate

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 Passed By ReferencePassed By Copy Object class Foo { private $f; } $foo = new Foo(); foobar($foo); Scalar string $s = “string”; foobar($s); Number $n = 1; foobar($n); Array $a = [1, 1, 2, 3]; foobar($a);

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

34 Hintable Types Can SpecifyCannot Specify array function foo(array $foo) {} Class Name function foo(Exception $e) {} Interface Name function foo(Nameable $n) {} String Number Scalar Trait

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()) {$stmt->fetch() print_r($row); } foreach ( $stmt->fetchAll() as $row) { print_r($row) }$stmt->fetchAll()

42 PDO lifecycle Create Handle Prepare Statement Execute Statement Fetch


Download ppt "Object Oriented Programming in PHP. Topics Quick OOP Review Classes Magic Methods Static Methods Inheritance Exceptions Interfaces Operators Type Hinting."

Similar presentations


Ads by Google