SlideShare a Scribd company logo
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";
?>
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);
• 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
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).
<html>
<body>
<?php
$handle=fopen("welcome.txt","r") ;
if($handle)
{
echo “File opened ok.”;
}
?>
</body>
</html>
• 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:
<?php
$file = fopen("welcome.txt", "r") or exit("Unable to
open file!");
//Output a line of the file until the end is reached
while(!feof($file))
{
echo fgets($file). "<br />";
}
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:
<?php
$file=fopen("welcome.txt","r");
while (!feof($file))
{
echo fgetc($file);
}
fclose($file);
?>
• The newline characters from the file were simply sent to
the browser, which doesn’t display newline characters
• To convert them to <br> elements instead
Example
<?php
$file=fopen("welcome.txt","r") or exit("Unable to open
file!");
while ($ch=fgetc($file))
{
if($ch==“n”){
$ch=“<br>”;
}
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(“http://www.php.net”);
$ft=str_replace(“n”,”<br>”,$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, “<br>”;
}
?>
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, “<br>”;
}
}
?>
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";
?>
UNIT-IV WT web technology for 1st year cs
UNIT-IV WT web technology for 1st year cs
UNIT-IV WT web technology for 1st year cs

More Related Content

Similar to UNIT-IV WT web technology for 1st year cs

Intro to Objective C
Intro to Objective CIntro to Objective C
Intro to Objective CAshiq Uz Zoha
 
Presentation 3rd
Presentation 3rdPresentation 3rd
Presentation 3rdConnex
 
Python's dynamic nature (rough slides, November 2004)
Python's dynamic nature (rough slides, November 2004)Python's dynamic nature (rough slides, November 2004)
Python's dynamic nature (rough slides, November 2004)Kiran Jonnalagadda
 
web programming Unit VI PPT by Bhavsingh Maloth
web programming Unit VI PPT  by Bhavsingh Malothweb programming Unit VI PPT  by Bhavsingh Maloth
web programming Unit VI PPT by Bhavsingh MalothBhavsingh Maloth
 
Introducing object oriented programming (oop)
Introducing object oriented programming (oop)Introducing object oriented programming (oop)
Introducing object oriented programming (oop)Hemlathadhevi Annadhurai
 
Java-Intro.pptx
Java-Intro.pptxJava-Intro.pptx
Java-Intro.pptxVijalJain3
 
Unit3 part3-packages and interfaces
Unit3 part3-packages and interfacesUnit3 part3-packages and interfaces
Unit3 part3-packages and interfacesDevaKumari Vijay
 
PHP - Introduction to Object Oriented Programming with PHP
PHP -  Introduction to  Object Oriented Programming with PHPPHP -  Introduction to  Object Oriented Programming with PHP
PHP - Introduction to Object Oriented Programming with PHPVibrant Technologies & Computers
 
Csharp introduction
Csharp introductionCsharp introduction
Csharp introductionSireesh K
 
9780538745840 ppt ch10
9780538745840 ppt ch109780538745840 ppt ch10
9780538745840 ppt ch10Terry Yoast
 
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering CollegeObject Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering CollegeDhivyaa C.R
 
java-corporate-training-institute-in-mumbai
java-corporate-training-institute-in-mumbaijava-corporate-training-institute-in-mumbai
java-corporate-training-institute-in-mumbaiUnmesh Baile
 
java-corporate-training-institute-in-mumbai
java-corporate-training-institute-in-mumbaijava-corporate-training-institute-in-mumbai
java-corporate-training-institute-in-mumbaivibrantuser
 

Similar to UNIT-IV WT web technology for 1st year cs (20)

Intro to Objective C
Intro to Objective CIntro to Objective C
Intro to Objective C
 
Presentation 3rd
Presentation 3rdPresentation 3rd
Presentation 3rd
 
Python's dynamic nature (rough slides, November 2004)
Python's dynamic nature (rough slides, November 2004)Python's dynamic nature (rough slides, November 2004)
Python's dynamic nature (rough slides, November 2004)
 
web programming Unit VI PPT by Bhavsingh Maloth
web programming Unit VI PPT  by Bhavsingh Malothweb programming Unit VI PPT  by Bhavsingh Maloth
web programming Unit VI PPT by Bhavsingh Maloth
 
Oops concept on c#
Oops concept on c#Oops concept on c#
Oops concept on c#
 
Introducing object oriented programming (oop)
Introducing object oriented programming (oop)Introducing object oriented programming (oop)
Introducing object oriented programming (oop)
 
Java-Intro.pptx
Java-Intro.pptxJava-Intro.pptx
Java-Intro.pptx
 
Unit3 part3-packages and interfaces
Unit3 part3-packages and interfacesUnit3 part3-packages and interfaces
Unit3 part3-packages and interfaces
 
PHP - Introduction to Object Oriented Programming with PHP
PHP -  Introduction to  Object Oriented Programming with PHPPHP -  Introduction to  Object Oriented Programming with PHP
PHP - Introduction to Object Oriented Programming with PHP
 
Csharp introduction
Csharp introductionCsharp introduction
Csharp introduction
 
9780538745840 ppt ch10
9780538745840 ppt ch109780538745840 ppt ch10
9780538745840 ppt ch10
 
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering CollegeObject Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
 
UNIT III (8).pptx
UNIT III (8).pptxUNIT III (8).pptx
UNIT III (8).pptx
 
UNIT III (8).pptx
UNIT III (8).pptxUNIT III (8).pptx
UNIT III (8).pptx
 
PHP 5
PHP 5PHP 5
PHP 5
 
java-corporate-training-institute-in-mumbai
java-corporate-training-institute-in-mumbaijava-corporate-training-institute-in-mumbai
java-corporate-training-institute-in-mumbai
 
java-corporate-training-institute-in-mumbai
java-corporate-training-institute-in-mumbaijava-corporate-training-institute-in-mumbai
java-corporate-training-institute-in-mumbai
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Oops
OopsOops
Oops
 
OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)
 

More from javed75

Unit-1.pptx final unit new mtech unit thre
Unit-1.pptx final unit new mtech unit threUnit-1.pptx final unit new mtech unit thre
Unit-1.pptx final unit new mtech unit threjaved75
 
javed_prethesis2608 on predcition of heart disease
javed_prethesis2608 on predcition of heart diseasejaved_prethesis2608 on predcition of heart disease
javed_prethesis2608 on predcition of heart diseasejaved75
 
presentationfinal-090714235255-phpapp01 (1) (2).pptx
presentationfinal-090714235255-phpapp01 (1) (2).pptxpresentationfinal-090714235255-phpapp01 (1) (2).pptx
presentationfinal-090714235255-phpapp01 (1) (2).pptxjaved75
 
algocomplexity cost effective tradeoff in
algocomplexity cost effective tradeoff inalgocomplexity cost effective tradeoff in
algocomplexity cost effective tradeoff injaved75
 
Section 7.5 version 2 AM new ppt for every
Section 7.5 version 2 AM new ppt for everySection 7.5 version 2 AM new ppt for every
Section 7.5 version 2 AM new ppt for everyjaved75
 
Cyber_Security_Awareness_Presentation (1).pptx
Cyber_Security_Awareness_Presentation (1).pptxCyber_Security_Awareness_Presentation (1).pptx
Cyber_Security_Awareness_Presentation (1).pptxjaved75
 
anand ethics ppt for phd scholar integral
anand ethics ppt for phd scholar integralanand ethics ppt for phd scholar integral
anand ethics ppt for phd scholar integraljaved75
 
Data Science.pptx NEW COURICUUMN IN DATA
Data Science.pptx NEW COURICUUMN IN DATAData Science.pptx NEW COURICUUMN IN DATA
Data Science.pptx NEW COURICUUMN IN DATAjaved75
 
1 Basic E-Commerce Concepts for it 2ndt year
1 Basic E-Commerce Concepts for it 2ndt year1 Basic E-Commerce Concepts for it 2ndt year
1 Basic E-Commerce Concepts for it 2ndt yearjaved75
 
training about android installation and usa
training about android installation and usatraining about android installation and usa
training about android installation and usajaved75
 
Phd2023-2024cIntegralUniversitynida.pptx
Phd2023-2024cIntegralUniversitynida.pptxPhd2023-2024cIntegralUniversitynida.pptx
Phd2023-2024cIntegralUniversitynida.pptxjaved75
 

More from javed75 (11)

Unit-1.pptx final unit new mtech unit thre
Unit-1.pptx final unit new mtech unit threUnit-1.pptx final unit new mtech unit thre
Unit-1.pptx final unit new mtech unit thre
 
javed_prethesis2608 on predcition of heart disease
javed_prethesis2608 on predcition of heart diseasejaved_prethesis2608 on predcition of heart disease
javed_prethesis2608 on predcition of heart disease
 
presentationfinal-090714235255-phpapp01 (1) (2).pptx
presentationfinal-090714235255-phpapp01 (1) (2).pptxpresentationfinal-090714235255-phpapp01 (1) (2).pptx
presentationfinal-090714235255-phpapp01 (1) (2).pptx
 
algocomplexity cost effective tradeoff in
algocomplexity cost effective tradeoff inalgocomplexity cost effective tradeoff in
algocomplexity cost effective tradeoff in
 
Section 7.5 version 2 AM new ppt for every
Section 7.5 version 2 AM new ppt for everySection 7.5 version 2 AM new ppt for every
Section 7.5 version 2 AM new ppt for every
 
Cyber_Security_Awareness_Presentation (1).pptx
Cyber_Security_Awareness_Presentation (1).pptxCyber_Security_Awareness_Presentation (1).pptx
Cyber_Security_Awareness_Presentation (1).pptx
 
anand ethics ppt for phd scholar integral
anand ethics ppt for phd scholar integralanand ethics ppt for phd scholar integral
anand ethics ppt for phd scholar integral
 
Data Science.pptx NEW COURICUUMN IN DATA
Data Science.pptx NEW COURICUUMN IN DATAData Science.pptx NEW COURICUUMN IN DATA
Data Science.pptx NEW COURICUUMN IN DATA
 
1 Basic E-Commerce Concepts for it 2ndt year
1 Basic E-Commerce Concepts for it 2ndt year1 Basic E-Commerce Concepts for it 2ndt year
1 Basic E-Commerce Concepts for it 2ndt year
 
training about android installation and usa
training about android installation and usatraining about android installation and usa
training about android installation and usa
 
Phd2023-2024cIntegralUniversitynida.pptx
Phd2023-2024cIntegralUniversitynida.pptxPhd2023-2024cIntegralUniversitynida.pptx
Phd2023-2024cIntegralUniversitynida.pptx
 

Recently uploaded

Natalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in KrakówNatalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in Krakówbim.edu.pl
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptssuser9bd3ba
 
Top 13 Famous Civil Engineering Scientist
Top 13 Famous Civil Engineering ScientistTop 13 Famous Civil Engineering Scientist
Top 13 Famous Civil Engineering Scientistgettygaming1
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxR&R Consult
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxViniHema
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234AafreenAbuthahir2
 
A case study of cinema management system project report..pdf
A case study of cinema management system project report..pdfA case study of cinema management system project report..pdf
A case study of cinema management system project report..pdfKamal Acharya
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdfKamal Acharya
 
2024 DevOps Pro Europe - Growing at the edge
2024 DevOps Pro Europe - Growing at the edge2024 DevOps Pro Europe - Growing at the edge
2024 DevOps Pro Europe - Growing at the edgePaco Orozco
 
fundamentals of drawing and isometric and orthographic projection
fundamentals of drawing and isometric and orthographic projectionfundamentals of drawing and isometric and orthographic projection
fundamentals of drawing and isometric and orthographic projectionjeevanprasad8
 
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptxCloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptxMd. Shahidul Islam Prodhan
 
Online resume builder management system project report.pdf
Online resume builder management system project report.pdfOnline resume builder management system project report.pdf
Online resume builder management system project report.pdfKamal Acharya
 
ENERGY STORAGE DEVICES INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES  INTRODUCTION UNIT-IENERGY STORAGE DEVICES  INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES INTRODUCTION UNIT-IVigneshvaranMech
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Aryaabh.arya
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdfPratik Pawar
 
Fruit shop management system project report.pdf
Fruit shop management system project report.pdfFruit shop management system project report.pdf
Fruit shop management system project report.pdfKamal Acharya
 
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...Amil baba
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdfKamal Acharya
 
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdfA CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdfKamal Acharya
 

Recently uploaded (20)

Natalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in KrakówNatalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in Kraków
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
 
Top 13 Famous Civil Engineering Scientist
Top 13 Famous Civil Engineering ScientistTop 13 Famous Civil Engineering Scientist
Top 13 Famous Civil Engineering Scientist
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
A case study of cinema management system project report..pdf
A case study of cinema management system project report..pdfA case study of cinema management system project report..pdf
A case study of cinema management system project report..pdf
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
2024 DevOps Pro Europe - Growing at the edge
2024 DevOps Pro Europe - Growing at the edge2024 DevOps Pro Europe - Growing at the edge
2024 DevOps Pro Europe - Growing at the edge
 
fundamentals of drawing and isometric and orthographic projection
fundamentals of drawing and isometric and orthographic projectionfundamentals of drawing and isometric and orthographic projection
fundamentals of drawing and isometric and orthographic projection
 
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptxCloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
 
Online resume builder management system project report.pdf
Online resume builder management system project report.pdfOnline resume builder management system project report.pdf
Online resume builder management system project report.pdf
 
ENERGY STORAGE DEVICES INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES  INTRODUCTION UNIT-IENERGY STORAGE DEVICES  INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES INTRODUCTION UNIT-I
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
Fruit shop management system project report.pdf
Fruit shop management system project report.pdfFruit shop management system project report.pdf
Fruit shop management system project report.pdf
 
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
 
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdfA CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
 

UNIT-IV WT web technology for 1st year cs

  • 2. 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
  • 3. 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).
  • 4. Creating Class • Let's start with a simple example. Save the following in a fi • le called class.php <?php class Demo { } ?>
  • 5. 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
  • 6. 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 !”; } } ?>
  • 7. 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 !”; } } ?>
  • 8. 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(); ?>
  • 10. 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;
  • 12. 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.
  • 13. 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
  • 14. <?php class MyClass { const constant = 'constant value'; function showConstant() { echo self::constant . "n"; } } echo MyClass::constant . "n"; ?>
  • 15. 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
  • 16. 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 { } } ?>
  • 17. 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.
  • 18. • 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.
  • 19. • 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.
  • 20. Destructor • Destructor, is method that will be run when object is ended <?php class Demo { function __destruct { } } ?>
  • 21. Inheritance • There are many benefits of inheritance with PHP, the most common is simplifying and reducing instances of redundant code
  • 22. • PHP class definitions can optionally inherit from a parent class definition by using the extends clause. The syntax is as follows:
  • 23. • 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.
  • 24. • example inherit Books class and adds more functionality based on the requirement.
  • 25. Interfaces: • Interfaces are defined to provide a common function names to the implementors. • Syntax: • Syntax: Interface implementation
  • 26. 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.
  • 27. Abstract classes and interfaces • interface InterfaceName { • public function name(parameters); • public function name(parameters); • ... • } • class ClassName implements InterfaceName { ... PHP
  • 28. • abstract class ClassName { • abstract public function name(parameters); • ... • } PHP
  • 29. 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
  • 30. Final • If the class itself is being defined final then it cannot be extended.
  • 31. 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).
  • 33.
  • 34. • 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!
  • 35. 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: <?php $file = fopen("welcome.txt", "r") or exit("Unable to open file!"); //Output a line of the file until the end is reached while(!feof($file)) { echo fgets($file). "<br />"; } fclose($file); ?>
  • 36. 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: <?php $file=fopen("welcome.txt","r"); while (!feof($file)) { echo fgetc($file); } fclose($file); ?>
  • 37. • The newline characters from the file were simply sent to the browser, which doesn’t display newline characters • To convert them to <br> elements instead Example <?php $file=fopen("welcome.txt","r") or exit("Unable to open file!"); while ($ch=fgetc($file)) { if($ch==“n”){ $ch=“<br>”; } echo “$ch”; } fclose($file); ?>
  • 38. 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(“http://www.php.net”); $ft=str_replace(“n”,”<br>”,$text); echo $ft; ?>
  • 39. 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, “<br>”; } ?>
  • 40. 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, “<br>”; } } ?>
  • 41. Getting File Size • Use filesize function. • Syntax: • filesize(file name ); Example <?php echo “The file abc.txt is “, filesize(“abc.txt”), “bytes long.”; ?>
  • 42. Opening a file with readfile( ) in PHP • <?PHP • $file_contents = readfile("dictionary.txt"); print $file_contents; • ?>
  • 43. Count lines in a file <?php $file = "somefile.txt"; $lines = count(file($file)); echo "There are $lines lines in $file"; ?>