Classes and Objects Imran Rashid CTO at ManiWeber Technologies.

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Object-Oriented PHP (1)
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Inheritance and Polymorphism CS351 – Programming Paradigms.
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Inheritance using Java
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Martin Kruliš This is an Object Oriented system. If we change something, the users object by Martin Kruliš (v1.0)1.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Object Oriented Programming in PHP. Topics Quick OOP Review Classes Magic Methods Static Methods Inheritance Exceptions Interfaces Operators Type Hinting.
OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.
Inheritance in the Java programming language J. W. Rider.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Programming using C# for Teachers Introduction to Objects Reference Types Functions of Classes Attributes and Types to a class LECTURE 2.
Programming in Java CSCI-2220 Object Oriented Programming.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Object Oriented Programming
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
21. PHP Classes To define a class, use the keyword class followed by the name and a block with the properties and method definitions Properties are declared.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
C++ General Characteristics: - Mixed typing system - Constructors and destructors - Elaborate access controls to class entities.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
UNIT-IV WT. Why use classes and objects? PHP is a primarily procedural language small programs are easily written without adding any classes or objects.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Static data members Constructors and Destructors
Advance OOP in PHP.
Inheritance and Polymorphism
Chapter 11: Inheritance and Polymorphism
PHP Classes and Objects
Chapter 11 Developing Object-Oriented PHP PHP Programming with MySQL Revised by A. Philipp – Spring 2010 (Rev SP’11)
Object Oriented Programming
Can perform actions and provide communication
Can perform actions and provide communication
Interface.
Java Programming Language
Lecture 22 Inheritance Richard Gesick.
Interfaces.
Chapter 12 Abstract Classes and Interfaces
Advanced Java Programming
CSCI 297 Scripting Languages Day Seven
Java – Inheritance.
Can perform actions and provide communication
Java Inheritance.
Object-Oriented Programming in PHP
Fundaments of Game Design
Chapter 14 Abstract Classes and Interfaces
Object-Oriented PHP (1)
Final and Abstract Classes
Lecture 10 Concepts of Programming Languages
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Presentation transcript:

Classes and Objects Imran Rashid CTO at ManiWeber Technologies

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

Introduction

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

The Basics

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)

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

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() ?>

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;     } } ?>

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

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::

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

Properties

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

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;

Class Constants

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

Constructors and Destructors

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();

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

Visibility

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)

Visibility Property Declaration

Visibility Property Declaration (Inheritence)

Object Inheritance

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

Scope Resolution Operator (::)

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

Static Keyword

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

Final Keyword

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

Late Static Bindings

Late Static Bindings self:: vs static::

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)?

Any ?