Presentation is loading. Please wait.

Presentation is loading. Please wait.

Classes and Objects Imran Rashid CTO at ManiWeber Technologies.

Similar presentations


Presentation on theme: "Classes and Objects Imran Rashid CTO at ManiWeber Technologies."— Presentation transcript:

1 Classes and Objects Imran Rashid CTO at ManiWeber Technologies

2 Outline Introduction The Basics Properties Class Constants
Constructors and Destructors Visibility Object Inheritance Scope Resolution Operator (::) Static Keyword Final Keyword Late Static Bindings

3 Introduction

4 Introduction Starting with PHP 5, the object model was rewritten to allow for better performance and more features This was a major change from PHP 4 PHP 5 has a full object model Among the features in PHP 5 are the inclusions of visibility, abstract and final classes and methods, additional magic methods, interfaces, cloning and typehinting PHP treats objects in the same way as references or handles, meaning that each variable contains an object reference rather than a copy of the entire object

5 The Basics

6 The Basics class Basic class definitions begin with the keyword class, followed by a class name, followed by a pair of curly braces which enclose the definitions of the properties and methods belonging to the class A valid class name starts with a letter or underscore, followed by any number of letters, numbers, or underscores A class may contain its own constants, variables (called "properties"), and functions (called "methods") It’s a good practice to use capital camel case for class names (e.g Student, DataStorage, MyDbCollection)

7 The Basics Simple Class
<?php class SimpleClass {     // property declaration     public $var = 'a default value';     // method declaration     public function displayVar() {         echo $this->var;     } } ?>

8 The Basics new To create an instance of a class, the we use new keyword An object will always be created unless the object has a constructor defined that throws an exception on error Classes should be defined before instantiation If a string containing the name of a class is used with new, a new instance of that class will be created <?php $instance = new SimpleClass(); // This can also be done with a variable: $className = 'SimpleClass'; $instance = new $className(); // new SimpleClass() ?>

9 The Basics $this The pseudo-variable $this is available when a method is called from within an object context $this is a reference to the calling object <?php class SimpleClass {     // property declaration     public $var = 'a default value';     // method declaration     public function displayVar() {         echo $this->var;     } } ?>

10 The Basics Properties and Methods
Class properties and methods live in separate "namespaces", so it is possible to have a property and a method with the same name class Foo {     public $bar = 'property';          public function bar() {         return 'method';     } } $obj = new Foo(); echo $obj->bar .''<br/>''. $obj->bar(); Output: property method

11 The Basics extends A class can inherit the methods and properties of another class by using the keyword extends in the class declaration It is not possible to extend multiple classes; a class can only inherit from one base class The inherited methods and properties can be overridden by redeclaring them with the same name defined in the parent class However, if the parent class has defined a method as final, that method may not be overridden It is possible to access the overridden methods or static properties by referencing them with parent::

12 The Basics Simple Class Inheritance
<?php class SimpleClass {     // property declaration     public $var = 'a default value';     // method declaration     public function displayVar() {         echo $this->var;     } } ?> class ExtendClass extends SimpleClass {     // Redefine the parent method     function displayVar()     {         echo "Extending class\n";         parent::displayVar();     } } $extended = new ExtendClass(); $extended->displayVar(); Output: Extending class a default value

13 Properties

14 Properties Class member variables are called "properties"
You may also see them referred to using other terms such as "attributes" or "fields", but for the purposes of this reference we will use "properties“ They are defined by using one of the keywords public, protected, or private, followed by a normal variable This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated Default is public

15 Properties Property Declarations
public $var1 = 'hello ' . 'world'; public $var7 = [true, false]; public $var2 = <<<EOD hello world EOD; public $var8 = <<<'EOD' hello world EOD; public $var3 = 1+2; public $var4 = self::static_func(); public $var6 = CONS_PI; public $var5 = $myVar;

16 Class Constants

17 Class Constants Note that class constants are allocated once per class, and not for each class instance

18 Constructors and Destructors

19 Constructors and Destructors
Note: Parent constructors are not called implicitly if the child class defines a constructor & in order to run a parent constructor, a call to parent::__construct() within the child constructor is required class BaseClass {    function __construct() { //AKA Magic        print "In BaseClass constructor\n";    } } class SubClass extends BaseClass {    function __construct() {        parent::__construct();        print "In SubClass constructor\n";    } } class Last extends BaseClass { } // In BaseClass constructor $obj = new BaseClass(); // In BaseClass constructor // In SubClass constructor $obj = new SubClass(); $obj = new Last();

20 Constructors and Destructors
class MyDestructableClass {    function __construct() {        print "In constructor<br />";    }    function __destruct() { //AKA Magic        print "In destructor<br />";    } } $obj = new MyDestructableClass();

21 Visibility

22 Visibility The visibility of a property, a method or (as of PHP 7.1.0) a constant can be defined by prefixing the declaration with the keywords public, protected or private Class members declared public can be accessed everywhere Members declared protected can be accessed only within the class itself and by inheriting and parent classes Members declared as private may only be accessed by the class that defines the member Class properties must be defined as public, private, or protected (var or not assigned is public)

23 Visibility Property Declaration

24 Visibility Property Declaration (Inheritence)

25 Object Inheritance

26 Object Inheritance This principle will affect the way many classes and objects relate to one another

27 Scope Resolution Operator (::)

28 Scope Resolution Operator (::)
Is a token that allows access to static, constant, and overridden properties or methods of a class

29 Static Keyword

30 Static Keyword Declaring class properties or methods as static makes them accessible without needing an instantiation class Foo {     public static function aStaticMethod() {         // ...     } } Foo::aStaticMethod(); $classname = 'Foo'; $classname::aStaticMethod(); // As of PHP 5.3.0

31 Final Keyword

32 Final Keyword PHP 5 introduces the final keyword, which prevents child classes from overriding a method by prefixing the definition with final & if the class itself is being defined final then it cannot be extended

33 Late Static Bindings

34 Late Static Bindings self:: vs static::

35 ASSIGNMENT Write the functionality of all these constructs & submit in written form on 31/12/2017 (first 10 minutes only) Q1: Read the following things from php manual (with code): require() require_once() include() include_once() die() exit Q2: How to combine/reference a class in student.php class which is written in another file called person.php class (write complete code of both classes)?

36 Any ?


Download ppt "Classes and Objects Imran Rashid CTO at ManiWeber Technologies."

Similar presentations


Ads by Google