440 likes | 582 Views
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).
E N D
Topics • Quick OOP Review • Classes • Magic Methods • Static Methods • Inheritance • Exceptions • Interfaces • Operators • Type Hinting • Using PDO
What is an Object? Encapsulateddata with logic An object has properties (data) and methods (logic)
Not everything is an object in PHP But all objects have (a) class
What is a Class? A way of saying “all objects of this type share these attributes”
Our world has many objects (chairs, exams, students, etc.) but we can recognize tables because: All tables have legs and a surface
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()
Class Syntax • Class Example { • public $foo; • const $baz = 42; • public function bar() { • // put code here • } • }
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
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 __
__construct() • Constructor • Called when a new instance is created
__toString() • How to convert the object to a string • Used whenever you use your object with the print or . (concatenate) operators.
Class Constants • Constants cannot change • They exist at the CLASS level not in instances. • Class Foo { const bar = 42; } • Print Foo::bar;
Static Properties and Methods • Static members are CLASS level not instance level • class Foo { • public static $bar = “bar”; • public static function baz() {} • }
self • Used to access static members of the current class (uses the most derived class) • self::$bar
New Operators • new • -> • ::
new operator • Used to instantiate a class. • $foo = new Foo();
-> operator • Used to access a property or method of an instance. • $foo->bar(); • $foo->baz = 2;
:: 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;
Inheritance • A way of sharing commonbehaviour among classes • PHP is a single inheritance language • A class may have no parent or 1 parent only.
Inheritance Syntax • class Bar {} • class Foo extends Bar {}
Accessing the parent • class Foo extends Bar { • public function __construct() { • parent::__construct(); • parent::$foo; • } • }
An abstract class is a class you cannot instantiate abstract class
Correct • abstract class Foo { • public $foo; • }
Correct • abstract class Foo { • abstract public bar(); • }
Incorrect • class Foo { • abstract public bar(); • }
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.
Interface Syntax • interface Fooable { • public function foo(); • protected function bar(); • } • class Foo implements Fooable { • public function foo() {} • protected function bar() [}; • }
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.
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.
Type hinting lets us specify the type of a parameter. Type Hinting
New Key Words and Operator • $this • self:: • parent:: • -> • :: • new • static • class • interface • public • private • protected
PDO PHP Data Objects
PDO • A common API for doing database operations with any database. • Supports most databases with the same classes/functions
Create a Connection $dbh = new PDO( 'mysql:host=localhost;dbname=test', $user, $pass);
Create a Statement $stmt = $dbh->prepare( "INSERT INTO table(col1, col2) VALUES (?, ?)");
Execute the statement • $stmt->execute(array(1, 2)) • $stmt->execute()
Fetch Results • while ($row = $stmt->fetch()) { • print_r($row); • } • foreach ( $stmt->fetchAll() as $row) { print_r($row) }