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.

Slides:



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

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Object-Oriented PHP (1)
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
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.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
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.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
8 th Semester, Batch 2008 Department Of Computer Science SSUET.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
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.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
CITA 342 Section 1 Object Oriented Programming (OOP)
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Classes, Interfaces and Packages
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
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.
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
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.
OOP Basics Classes & Methods (c) IDMS/SQL News
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Classes (Part 1) Lecture 3
Object-Oriented Programming: Classes and Objects
7. Inheritance and Polymorphism
Andy Wang Object Oriented Programming in C++ COP 3330
Final and Abstract Classes
Table of Contents Class Objects.
Object-Oriented Programming & Design Lecture 14 Martin van Bommel
PHP Classes and Objects
Road Map Introduction to object oriented programming. Classes
PRINCIPALES OF OBJECT ORIENTED PROGRAMMING
Object-Oriented Programming: Classes and Objects
Nested class.
Java Programming Language
Week 6 Object-Oriented Programming (2): Polymorphism
Interfaces.
CSE 1030: Implementing GUI Mark Shtern.
Java – Inheritance.
Object-Oriented Programming in PHP
Software Engineering for Internet Applications
Object-Oriented Programming: Inheritance and Polymorphism
Fundaments of Game Design
Chapter 14 Abstract Classes and Interfaces
Object-Oriented PHP (1)
Final and Abstract Classes
Classes and Objects Imran Rashid CTO at ManiWeber Technologies.
Object Oriented PHP.
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Presentation transcript:

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 larger programs, however, become unordered with so many disorganized functions grouping related data and behavior into objects helps manage size and complexity

Object Oriented Concept  Classes, which are the "blueprints" for an object and are the actual code that defines the properties and methods.  Objects, which are running instances of a class and contain all the internal data and state information needed for your application to function.  Encapsulation, which is the capability of an object to protect access to its internal data  Inheritance, which is the ability to define a class of one kind as being a sub-type of a different kind of class (much the same way a square is a kind of rectangle).

Creating Class Let's start with a simple example. Save the following in a fi le called class.php <?php class Demo { } ?>

Constructing and using objects # construct an object $name = new ClassName(parameters); # access an object's field (if the field is public) $name->fieldName # call an object's method $name->methodName(parameters); PHP

Adding Method The Demo class isn't particularly useful if it isn't able to do anything, so let's look at how you can create a method. <?php class Demo { function SayHello($name) { echo “Hello $name !”; } ?>

Adding Properties Adding a property to your class is as easy as adding a method. <?php class Demo { public $name; function SayHello() { echo “Hello $this->$name !”; } ?>

Object Instantiation You can instantiate an object of type Demo like this: <?php require_once('class.php'); $objDemo = new Demo(); $objDemo->name = “mbstechinfo”; $objDemo->SayHallo(); ?>

Example on Class

Creating Objects in PHP Once you defined your class, then you can create as many objects: $physics = new Books; $maths = new Books; $chemistry = new Books;

Calling Member Functions

Protecting Access to Member Variables  There are three different levels of visibility that a member variable or method can have :  Public ▪ members are accessible to any and all code  Private ▪ members are only accessible to the class itself  Protected ▪ members are available to the class itself, and to classes that inherit from it Public is the default visibility level for any member variables or functions that do not explicitly set one, but it is good practice to always explicitly state the visibility of all the members of the class.

Class Constants  It is possible to define constant values on a per-class basis remaining the same and unchangeable.  Constants differ from normal variables in that you don't use the $ symbol to declare or use them  The value must be a constant expression, not (for example) a variable, a property, a result of a mathematical operation, or a function call

<?php class MyClass { const constant = 'constant value'; function showConstant() { echo self::constant. "\n"; } } echo MyClass::constant. "\n"; ?> <?php class MyClass { const constant = 'constant value'; function showConstant() { echo self::constant. "\n"; } } echo MyClass::constant. "\n"; ?>

Static Keyword Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object

Contructor Constructor is the method that will be implemented when object has been initiated Commonly, constructor is used to initialize the object Use function __construct to create constructor in PHP <?php class Demo { function __construct { } ?>

Constructor Functions: Constructor Functions are special type of functions which are called automatically whenever an object is created. PHP provides a special function called __construct() to define a constructor. You can pass as many as arguments you like into the constructor function.

Following example will create one constructor for Books class and it will initialize price and title for the book at the time of object creation.

Now we don't need to call set function separately to set price and title. We can initialize these two member variables at the time of object creation only.

Destructor Destructor, is method that will be run when object is ended <?php class Demo { function __destruct { } ?>

Inheritance There are many benefits of inheritance with PHP, the most common is simplifying and reducing instances of redundant code

PHP class definitions can optionally inherit from a parent class definition by using the extends clause. The syntax is as follows:

The effect of inheritance is that the child class (or subclass or derived class) has the following characteristics: Automatically has all the member variable declarations of the parent class. Automatically has all the same member functions as the parent, which (by default) will work the same way as those functions do in the parent.

example inherit Books class and adds more functionality based on the requirement.

Interfaces: Interfaces are defined to provide a common function names to the implementors. Syntax: Syntax: Interface implementation

Abstract Classes: An abstract class is one that cannot be instantiated, only inherited. You declare an abstract class with the keyword abstract, like this: Note : function definitions inside an abstract class must also be preceded by the keyword abstract.

Abstract classes and interfaces interface InterfaceName { public function name(parameters);... } class ClassName implements InterfaceName {... PHP

abstract class ClassName { abstract public function name(parameters);... } PHP

Abstract classes and interfaces interfaces are supertypes that specify method headers without implementations – cannot be instantiated; cannot contain function bodies or fields – enables polymorphism between subtypes without sharing implementation code abstract classes are like interfaces, but you can specify fields, constructors, methods – also cannot be instantiated; enables polymorphism with sharing of implementation code

Static Keyword: Declaring class members or methods as static makes them accessible without needing an instantiation of the class. A member declared as static can not be accessed with an instantiated class object (though a static method can).

Final If the class itself is being defined final then it cannot be extended.

PHP File Handling PHP Filesystem Introduction The filesystem functions allow you to access and manipulate the filesystem. Opening a File – The fopen() function is used to open files in PHP. The first parameter of this function contains the name of the file to be opened the second parameter specifies in which mode the file should be opened: If the fopen() function is unable to open the specified file, it returns 0 (false).

<?php $handle=fopen("welcome.txt","r") ; if($handle) { echo “File opened ok.”; } ?>

Closing a File The fclose() function is used to close an open file: Check End-of-file The feof() function checks if the "end-of-file" (EOF) has been reached. Cannot read from files opened in w, a, and x mode!

Reading a File Line by Line The fgets() function is used to read a single line from a file. After a call to this function the file pointer has moved to the next line. Example: "; } fclose($file); ?>

Reading a File Character by Character The fgetc() function is used to read a single character from a file. After a call to this function the file pointer moves to the next character. Example:

The newline characters from the file were simply sent to the browser, which doesn’t display newline characters To convert them to elements instead Example <?php $file=fopen("welcome.txt","r") or exit("Unable to open file!"); while ($ch=fgetc($file)) { if($ch==“\n”){ $ch=“ ”; } echo “$ch”; } fclose($file); ?>

Reading a whole file at once Use file_get_contents function. Syntax: file_get_contents(file name (or) file path); Example <?php $text=file_get_contents(“ $ft=str_replace(“\n”,” ”,$text); echo $ft; ?>

Reading a file into an Array Use file function. Syntax: file(file name (or) file path); Example <?php $text=file(“file.txt”); foreach($text as $number=>$line) { echo “Line $number: “, $line, “ ”; } ?>

Checking if a File Exists Use file_exists function. Syntax: file_exists(file name ); Example <?php $fname=“abc.txt”; If(file_exists($fname)){ $text=file($fname); foreach($text as $number=>$line) { echo “Line $number: “, $line, “ ”; } ?>

Getting File Size Use filesize function. Syntax: filesize(file name ); Example <?php echo “The file abc.txt is “, filesize(“abc.txt”), “bytes long.”; ?>

Opening a file with readfile( ) in PHP <?PHP $file_contents = readfile("dictionary.txt"); print $file_contents; ?>

Count lines in a file <?php $file = "somefile.txt"; $lines = count(file($file)); echo "There are $lines lines in $file"; ?>