Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

PHP class notes-unit 3, Lecture notes of Programming Languages

php class notes unit 3 syllabus: Object oriented programming - Advanced object oriented programming

Typology: Lecture notes

2018/2019

Uploaded on 08/12/2019

krishnavalli-singaravelan
krishnavalli-singaravelan 🇮🇳

5 documents

1 / 33

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
UNIT -3..................................................................................................................................................... 1
Object Oriented Programming................................................................................................................................................. 1
Creang Classes..................................................................................................................................................... 2
Creang Objects..................................................................................................................................................... 2
Seng Access to Properes and Methods........................................................................................................................... 3
Using constructors to inialize Objects.................................................................................................................................4
Using Destructors to Clean Up..............................................................................................................................................6
Basing One Class on Another with Inheritance.....................................................................................................................6
Constructors and Inheritance............................................................................................................................................... 8
Over Riding Methods..................................................................................................................................................... 9
Over Loading Methods..................................................................................................................................................... 9
Auto loading Classes..................................................................................................................................................... 10
Advanced Object Oriented Programming...............................................................................................................................11
Creang Stac Methods..................................................................................................................................................... 11
Creang Abstract classes.................................................................................................................................................... 13
Creang Interfaces..................................................................................................................................................... 14
Comparing Objects..................................................................................................................................................... 16
Creang Class Constants.....................................................................................................................................................17
Using nal keyword..................................................................................................................................................... 18
Cloning Objects..................................................................................................................................................... 19
Reecon..................................................................................................................................................... 20
PHP: UNIT 3 |Page 1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21

Partial preview of the text

Download PHP class notes-unit 3 and more Lecture notes Programming Languages in PDF only on Docsity!

  • UNIT -3.....................................................................................................................................................
  • Object Oriented Programming.................................................................................................................................................
    • Crea�ng Classes.....................................................................................................................................................
    • Crea�ng Objects.....................................................................................................................................................
    • Se�ng Access to Proper�es and Methods...........................................................................................................................
    • Using constructors to ini�alize Objects.................................................................................................................................
    • Using Destructors to Clean Up..............................................................................................................................................
    • Basing One Class on Another with Inheritance.....................................................................................................................
    • Constructors and Inheritance...............................................................................................................................................
    • Over Riding Methods.....................................................................................................................................................
    • Over Loading Methods.....................................................................................................................................................
    • Auto loading Classes.....................................................................................................................................................
  • Advanced Object Oriented Programming...............................................................................................................................
    • Crea�ng Sta�c Methods.....................................................................................................................................................
    • Crea�ng Abstract classes....................................................................................................................................................
    • Crea�ng Interfaces.....................................................................................................................................................
    • Comparing Objects.....................................................................................................................................................
    • Crea�ng Class Constants.....................................................................................................................................................
    • Using final keyword.....................................................................................................................................................
    • Cloning Objects.....................................................................................................................................................
    • Reflec�on.....................................................................................................................................................

UNIT -

Object Oriented Programming

  • Php is not designed to be an object oriented language, but it has incredible number of oop features built into it.
  • OOP is targeted for larger applications.
  • In OOP, the code is broken up into objects.
  • Objects usually include asset of functions and variables related to it, wrapped up inside the object.
  • Functions in OOP are called as methods and data items are called properties.
  • An object can hide all its methods and properties internally and expose some methods for public use.
  • To create an object, first a class has to be created.
  • Object are to classes what names are to humans.
  • Object is called an instance of a class.
  • More than one object can be created for a class.

Creating Classes

  • Classes should be created before any instance(object) is created for it.
  • The structure of a class. class { var $; function func_nm([args]) {... } }
  • In php, classes are usually given names that start with upper case letter and object names start with lower case letters.
  • E.g.: class Person
  • (^) All methods and properties of person class can be accessed using $Ralph class.
  • The methods in the class are called using ‘->’ operator. E.g.: $ralph -> set_name (“Ralph”);
  • To access the properties in a class, $ = -> ;
  • So to recover the name stored in the class, $ralph -> get_data(); Or $ralph -> name; Can be used.
  • Advantage of using class:
    • Data’s in object are not accessed directly, methods are used.
    • Methods that return values of properties are called accessor methods.

Se�ng Access to Proper�es and Methods...........................................................................................................................

  • The members of a class are declared as public by default, means, those members can be accessed from anywhere in the code.
  • To restrict the access to the members of a class or object with the php access modifiers.
  • The various access modifiers are:
    • Public
      • default modifier
      • accessible to all
    • Private
      • Accessible in the same class.
    • Protected.
      • Accessible in the same class and classes derived from that class.

Public Access

  • Public access is the most unrestricted access of all.
  • It is the default access.
  • Methods and properties can be declared public explicitly. E.g.: Program 3.
name = $data; } public function get_name() { return $this -> name; } } $a1 = new Test; $a1 -> set_name ("India"); echo "Accessing property directly:" , $a1 -> name; ?>
  • In the above program,
    • The property $name is declared as public explicitly.
    • (^) As public access is the default access. “var $name” and “public $name” works the same way.
    • Like wise Public function and function are same.
    • The property of the person class is accessed directly.

Private Access

  • A method or property of a class can be made private with the private keyword.
  • When an object member is made private, they are not accessible outside the class. E.g.: Program 3. <?php class Test
  • (^) Construct (i.e.“Construct” proceeded by two under scores.)
  • Constructors can take any number of arguments; those arguments can be assigned to the properties of the class.
  • The arguments are passed, when new objects are created. E.g.: $ra = new person (“a”, 7);
  • To assign the argument data to the internal name stored in the object, this keyword followed by -> operator is used. E.g.: $this -> name = $dt; E.g: .: Program 3.
name = $dt; } function set_name($data) { $this -> name = $data; } function get_name() { return $this -> name; } } $dan = new person ("dan"); echo "name =", $dan -> get_name(); $dan->set_name("Jeff"); echo "
Name =", $dan -> get_name(); ?> - In the above program, - The class person has 2 functions and a constructor function. - When a new object is created for the class person, the argument “Dan” is passed to the constructor. And the code inside the constructor is carried out.
  • (^) A constructor can accept any number of arguments, as long as the constructor is set up to take those arguments.
  • All php classes come with a default constructor that takes no arguments. It is the default constructor that is called, when no constructor is specified.
  • As soon as a user defined constructor is created, the default constructor is no longer accessible.
  • The constructor can also have the name of its class. E.g.:

class person { var $name; function person ($data) {



} } Here “_ _construct” is replaced with “person”.

Using Destructors to Clean Up..............................................................................................................................................

  • (^) These are functions that are called under two circumstances:
    • When an object is destroyed.
    • When all references to the object go out of scope.
  • Destructors are named “_ _destruct” in php. Syntax: function _ _destruct () { ------- ------- } E.g.: .: Program 3.

Now the methods in the base class can be accessed in the derived class. E.g.: .: Program 3.

func_A"; } } Class B extends A { function func_b() { echo " \n In B -> func_B"; } } $no = new B; $no -> func_A (); $no -> func_B (); ?> In the above program,
  • The class A is the Base class to B.
  • An object is created for B class.
  • The method func_a is accessed through the object created for class B.
  • The derived class will inherit the functionality of the base class.
  • (^) The members of a class declared as private cannot be accessed in the derived class.
  • (^) Protected members can be accessed from the derived class but not outside the class.
  • To set a method as protected, the protected keyword is prefixed with the name of the function. E.g.: Protected function func_A() {

    } - To make a protected function accessible to code outside the object. The following method is used. - Create a new method in derived class to call the base class function. E.g.: Program 3. <?php class A { protected function func_A() { echo "in function A"; } } class b extends A { function func_B() { echo "In function B"; $this -> func_A(); } } $t = new B; $t -> func_B(); ?>

$bon = New Friend; ?> In the above program,

  • The class’s person & friend have a constructor written in it.
  • In the main program, an object is declared for the friend class
  • It would call the constructor of friend class, which in term will call the person class constructor using the statement: Parent: _ _construct ()
  • Similarly a function can be called likewise. function set_name_public ($data) { parent :set_name($data); }
  • Base class methods and properties can be called from derived class using the “parent ::” syntax.

Over Riding Methods.....................................................................................................................................................

  • Over riding methods means redefining a base class method in a derived class. E.g.: Program 3. <?php class person { var $nm; function set_name($data) { $this -> name = $data; echo $this -> name; } } class friend extends person {

var $name; function set_name($data) { $this -> name = strtoupper ($data); echo $this -> name; } } $fr = new friend; $fr -> set_name ("John"); ?>

  • (^) In the above program,
    • A method with the same name is created in both the classes.
    • Class friend inherits the members of class person.
    • A new object is created for the friend class.
    • The set_name() method is called through friend class object.
    • The set_name() method of friend class has the higher priority than the person class, because, object is created for the friend class.
    • (^) So, the set_name () method of person class is overridden by the method in friend class.

Over Loading Methods.....................................................................................................................................................

  • Overloading means creating an alternate version with a different argument list.
  • Normally. There would be two methods with different argument list.
  • In php. Method overloading is carried out using “__Call” method.
  • __Call is the method that is called when a method is called that doesn’t exist.
  • The __call method accepts 2 arguments.
    1. Name of the method called.
    2. An array holding the argument lists that was passed to the missing method. Syntax: function __call ($method, $arguments) {
  • In the above program,
    • The class friend has two properties.
    • A method display () to display the vales of the variables.
    • A __call method for non-existing methods.
    • In the main program, when the statement. $ft -> set_name (“Susan”) is encountered, the __call () method is called and the code inside them is executed.

Auto loading Classes.....................................................................................................................................................

  • The autoload a function that is called whenever an object is called for a non-existing class.
  • (^) Whenever the class definition is too longer or there are a lot number of classes, they can be written in separate files.
  • These class files are included in the program using include or require inside __autoload() function.
  • Eg: Person.php <?php Class Person { Var $nm; Function set_name($data) { $this -> nm = $data; } } ?>

Autoload.php

$to -> set_name(“Tony”); ?>

  • In the above program,
    • Friend Class is in a separate file, for convenience, the name of that file is same as that of the class.
    • Run the autoload.php program, the first statement. $to = new Friend; Will check for the Friend Class that is non-existent.
    • In such a situation, the __autoload function is called with the name of the non-existent class.
    • Inside the __autoload function the class file is included to the main program.

Advanced Object Oriented Programming

Creating Static Methods

  • Static methods are methods that can be called by referencing the class.
  • (^) Static methods can be called without having to create object for its class.
  • To make a method static, the “static” keyword is added in front of function keyword.
  • Syntax: Public static function <func.nm ()> { ----- }
  • To access a static method, it is referenced directly using the class name followed by :: (scope resolution operator)
  • (^) Then the method name with arguments if any.
  • Syntax: :: <function nm([args])>;
  • The -> operator is not used as there is no object involved. Instead :: , a scope resolution operator is used.
  • E.g.: Program 3.

self :: $dt = $msg } } math :: set_data(7); ?>

  • “$this -> data” syntax cannot be used here, since $this refers to the current object and there is no object and there is no object here.
  • Instead self keyword is used, which refers to the current class.

Static members and Inheritance

  • Static members of a base class can be referred from inherited class.
  • E.g.: Program 3. <?php class Math { Static $msg = "Math Class"; } class NewMath extends math { public static function show_msg() { echo "Msg from :......" , Math :: $msg; } } echo "accessing math from NewMath:"; NewMath :: show_msg(); ?>
  • In the above program,
  • A static property is present in math class.
  • The method in NewMath is set as static.
  • NewMath inherits the properties and methods of Math class.
  • (^) The show_msg() is called using class.
  • The show_msg() in turn gets value from base class using ‘reference with class’.
  • Declaring a property as static doesn’t affect the inheritance properties.
  • Only the way of referring to methods and properties will changes.

Creating Abstract classes

  • An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.
  • Some methods in abstract class can be marked as abstract.
  • When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract.
  • Syntax: abstract class { ------- }
  • To add a abstract method, abstract function ; format is used, no definition for the function is needed here. E.g.: Program 3.