You are on page 1of 26

UNIT IV

CHAPTER I

USING FUNCTIONS AND CLASSES

PHP USER DEFINED FUNCTION

 In all programming and scripting language, a function is a block of statements that can be used
repeatedly in a program. In PHP, the concept of the function is the same as in other language like
'C'. 
 There are more than 1,000 in-built functions into the standard PHP distribution. Besides these we can
define functions as per our requirements. These are called 'User Defined Function'.
SYNTAX:

Function function-name ()
 {
    statement 1 :
    statement 2 :
    statement 3 :
     ......
 }

Elements of function
 Function: A function declaration starts with the special word 'function'.
 Name of the function: The function name is defined by the user.A valid function name starts with a
letter or underscore, followed by any number of letters, numbers, or under scores.Remember that
function names are case-insensitive.
 Opening and Closing curly braces ({  } ): The function body enclosed within a pair of braces which
may contain variable names and actual function code. The opening curly brace ( { ) indicates the
beginning of the function code and the closing curly ( } ) brace indicates the termination of the
function.
Example:
<?php  
  function myfunction()  
  {  
  echo "Good Morning";  
Department of Computer Science Page 1
  }  
  myfunction();  
  ?>  
Example:Functionswith in a function
<?php  
  function function1()  
  {  
  function function2()  
  {  
  echo "Good Morning <br>";  
  }  
  }  
  function1();  
  function2();  
  ?>  
PHP Function arguments, Returning Values
 PHP Function arguments
 In PHP information are passed to functions through argument list, which is a comma-
delimited list of expression.
 There are three different ways of passing arguments to a function, arguments by value (the
default), passing by reference, and default argument values.
 PHP Function arguments by values
 The argument can be any valid expression. The expression is evaluated and its value assigned
to the appropriate variable in the function. In the following function $a assigned the value 10
and $b assigned the value 20:
EXAMPLE:
Functionadd($a, $b)
{
...
}
add(10, 20);

 Passing arguments By Refernce


 By default the function arguments are passed by value. If you want to allow a function to
change its arguments, you must pass the arguments by reference.

Department of Computer Science Page 2


 To pass an argument to a function as reference, simply add an ampersand (&) character
before the variable name.
EXAMPLE:
<?php  
  function cube(&$x)  
  {  
  $x = $x * $x * $x;  
  }  
$result = 5;  
  cube($result);  
  echo $result;  
  ?>  
OUTPUT
125
 Default Argument Value
 In the following example the function wage() use a default parameter.
 When we have called the function without arguments it takes the default value as argument.
EXAMPLE:
<?php  
  function wage($minwage= 100)  
  {  
  echo "The wage is : $minwage <br />" ;  
  }<  
wage(200);  
wage();  
wage(100);  
?>  
OUTPUT
The wage is : 200 
The wage is : 100 
The wage is : 100

 Php Returning Values


 In PHP values are returned by return statement. The return values must be specified in the
variable.

Department of Computer Science Page 3


 If the statement is called within a function the function is terminated immediately and pass
control back to previous position from which it was called.The return statement can return
any type of data.
EXAMPLE:
 In the following example a function accept a number and calculate the cube of that number
using return statement.
<?php  
  function cube($x)  
  {  
  return $x * $x * $x;  
  }  
  echo "The cube of 4 is : ".cube(4)."<br />";  
  echo "The cube of 9 is : ".cube(9)."<br />";  
  echo "The cube of 20 is : ".cube(20)."<br />";  
  ?>  
Output
The cube of 4 is : 64
The cube of 9 is : 729
The cube of 20 is : 8000

OBJECT ORIENTED PROGRAMMING IN PHP


Object Oriented Concepts:
Before we go in detail, lets define important terms related to Object Oriented Programming.
 Class: This is a programmer-defined data type, which includes local functions as well as local data.
You can think of a class as a template for making many instances of the same kind (or class) of
object.
 Object: An individual instance of the data structure defined by a class. You define a class once and
then make many objects that belong to it. Objects are also known as instance.
 Member Variable: These are the variables defined inside a class. This data will be invisible to the
outside of the class and can be accessed via member functions. These variables are called attribute of
the object once an object is created.
 Member function: These are the function defined inside a class and are used to access object data.
 Inheritance: When a class is defined by inheriting existing function of a parent class then it is called
inheritance. Here child class will inherit all or few member functions and variables of a parent class.

Department of Computer Science Page 4


 Parent class: A class that is inherited from by another class. This is also called a base class or super
class.
 Child Class: A class that inherits from another class. This is also called a subclass or derived class.
 Polymorphism: This is an object oriented concept where same function can be used for different
purposes. For example function name will remain same but it make take different number of
arguments and can do different task.
 Overloading: a type of polymorphism in which some or all of operators have different
implementations depending on the types of their arguments. Similarly functions can also be
overloaded with different implementation.
 Data Abstraction: Any representation of data in which the implementation details are hidden
(abstracted).
 Encapsulation: refers to a concept where we encapsulate all the data and member functions together
to form an object.
 Constructor: refers to a special type of function which will be called automatically whenever there
is an object formation from a class.
 Destructors: refers to a special type of function which will be called automatically whenever an
object is deleted or goes out of scope.
Defining PHP Classes:
The general form for defining a new class in PHP is as follows:

<?php
class phpClass{
var $var1;
var $var2 = "constant string";
function myfunc ($arg1, $arg2) {
[..]
}
[..]
}
?>

Here is the description of each line:


 The special form class, followed by the name of the class that you want to define.
 A set of braces enclosing any number of variable declarations and function definitions.
 Variable declarations start with the special form var, which is followed by a conventional $ variable
name; they may also have an initial assignment to a constant value.

Department of Computer Science Page 5


 Function definitions look much like standalone PHP functions but are local to the class and will be
used to set and access object data.
Example:
Here is an example which defines a class of Books type:

<?php
class Books{
/* Member variables */
var $price;
var $title;
/* Member functions */
function setPrice($par){
$this->price = $par;
}
function getPrice(){
echo $this->price ."<br/>";
}
function setTitle($par){
$this->title = $par;
}
function getTitle(){
echo $this->title ." <br/>";
}
}
?>

The variable $this is a special variable and it refers to the same object ie. itself.
Creating Objects in PHP
 Once you defined your class, then you can create as many objects as you like of that class type.
Following is an example of how to create object using new operator.

$physics = new Books;


$maths = new Books;
$chemistry = new Books;

 Here we have created three objects and these objects are independent of each other and they
will have their existence separately.
 Next we will see how to access member function and process member variables.

Department of Computer Science Page 6


The relationship between classes and objects

Calling Member Functions


 After creating your objects, you will be able to call member functions related to that object.
 One member function will be able to process member variable of related object only.Following
example shows how to set title and prices for the three books by calling member functions.

$physics->setTitle( "Physics for High School" );


$chemistry->setTitle( "Advanced Chemistry" );
$maths->setTitle( "Algebra" );

$physics->setPrice( 10 );
$chemistry->setPrice( 15 );
$maths->setPrice( 7 );

Now you call another member functions to get the values set by in above example:

$physics->getTitle();
$chemistry->getTitle();
$maths->getTitle();
$physics->getPrice();
$chemistry->getPrice();
$maths->getPrice();

This will produce following result:

Physics for High School


Advanced Chemistry
Algebra

Department of Computer Science Page 7


10
15
7

Constructor Functions:
 Constructor Functions are special type of functions which are called automatically whenever an
object is created.
 So we take full advantage of this behavior, by initializing many things through constructor functions.
 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.

function __construct( $par1, $par2 ){


$this->price = $par1;
$this->title = $par2;
}

 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. Check following example below:

$physics = new Books( "Physics for High School", 10 );


$maths = new Books ( "Advanced Chemistry", 15 );
$chemistry = new Books ("Algebra", 7 );

/* Get those set values */


$physics->getTitle();
$chemistry->getTitle();
$maths->getTitle();

$physics->getPrice();
$chemistry->getPrice();
$maths->getPrice();

This will produce following result:

Physics for High School


Advanced Chemistry
Algebra

Department of Computer Science Page 8


10
15
7

Destructor:
 Like a constructor function you can define a destructor function using function __destruct ().
 You can release all the resources with-in a destructor.
Inheritance:
 PHP class definitions can optionally inherit from a parent class definition by using the extends
clause. The syntax is as follows:

class Child extends Parent {


<definition body>
}

The effect of inheritance is that the child class (or subclass or derived class) has the following
characteristics:
 Automatically have all the member variable declarations of the parent class.
 Automatically has all the same a member function as the parent, which (by default) will work the
same way as those functions do in the parent.
 Following examples inherit Books class and adds more functionality based on the requirement.

class Novel extends Books{


var publisher;
function setPublisher($par){
$this->publisher = $par;
}
function getPublisher(){
echo $this->publisher. "<br />";
}
}

 Now apart from inherited functions, class Novel keeps two additional member functions.
Function Overriding:
 Function definitions in child classes override definitions with the same name in parent classes.
 In a child class, we can modify the definition of a function inherited from parent class.
 In the following example getPrice and getTitle functions are overridden to return some values.

function getPrice(){

Department of Computer Science Page 9


echo $this->price . "<br/>";
return $this->price;
}
function getTitle(){
echo $this->title . "<br/>";
return $this->title;
}

Public Members:
Unless you specify otherwise, properties and methods of a class are public. That is to say, they may be
accessed in three possible situations:
 From outside the class in which it is declared
 From within the class in which it is declared
 From within another class that implements the class in which it is declared
Till now we have seen all members as public members. If you wish to limit the accessibility of the members
of a class then you define class members as private or protected.

Private members:
 By designating a member private, you limit its accessibility to the class in which it is declared.
 The private member cannot be referred to from classes that inherit the class in which it is declared
and cannot be accessed from outside the class.
 A class member can be made private by using private keyword in front of the member.

class MyClass {
private $car = "skoda";
$driver = "SRK";

function __construct($par) {
// Statements here run every time
// an instance of the class
// is created.
}
function myPublicFunction() {
return("I'm visible!");
}
private function myPrivateFunction() {

Department of Computer Science Page 10


return("I'm not visible outside!");
}
}

 When MyClass class is inherited by another class using extends, myPublicFunction() will be visible,


as will $driver.
 The extending class will not have any awareness of or access to myPrivateFunction and $car,
because they are declared private.
Protected members:
 A protected property or method is accessible in the class in which it is declared, as well as in classes
that extend that class.
 Protected members are not available outside of those two kinds of classes. A class member can be
made protected by using protected keyword infront of the member.Here is different version of
MyClass:

class MyClass {
protected $car = "skoda";
$driver = "SRK";

function __construct($par) {
// Statements here run every time
// an instance of the class
// is created.
}
function myPublicFunction() {
return("I'm visible!");
}
protected function myPrivateFunction() {
return("I'm visible in child class!");
}
}

Interfaces:
 Interfaces are defined to provide a common function names to the implementers.
 Different implementers can implement those interfaces according to their requirements. You can say,
interfaces are skeltons which are implemented by developers.As of PHP5, it is possible to define an
interface, like this:

Department of Computer Science Page 11


interface Mail {
public function sendMail();
}

 Then, if another class implemented that interface, like this:

class Report implements Mail {


// sendMail() Definition goes here
}

Constants:
 A constant is somewhat like a variable, in that it holds a value, but is really more like a function
because a constant is immutable.
 Once you declare a constant, it does not change.Declaring one constant is easy, as is done in this
version of MyClass:

class MyClass {
constrequiredMargin = 1.7;
function __construct($incomingValue) {
// Statements here run every time
// an instance of the class
// is created.
}
}

 In this class, required Margin is a constant.


 It is declared with the keyword const, and under no circumstances can it be changed to anything
other than 1.7. Note that the constant's name does not have a leading $, as variable names do.
Abstract Classes:
 An abstract class is one that cannot be instantiated, only inherited.
 You declare an abstract class with the keyword abstract, like this: When inheriting from an abstract
class, all methods marked abstract in the parent's class declaration must be defined by the child;
additionally, these methods must be defined with the same visibility.

abstract class MyAbstractClass {


abstract function myAbstractFunction() {
}
}

 Note that function definitions inside an abstract class must also be preceded by the keyword abstract.
It is not legal to have abstract function definitions inside a non-abstract class.
Department of Computer Science Page 12
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).Try out following example:

<?php
class Foo
{
public static $my_static = 'foo';

public function staticValue() {


return self::$my_static;
}
}
print Foo::$my_static . "\n";
$foo = new Foo();
print $foo->staticValue() . "\n";

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.
 Following example results in Fatal error: Cannot override final method Base Class::more Testing()

<?php
class BaseClass {
public function test() {
echo "BaseClass::test() called<br>";
}

final public function moreTesting() {


echo "BaseClass::moreTesting() called<br>";
}
}

class ChildClass extends BaseClass {


public function moreTesting() {

Department of Computer Science Page 13


echo "ChildClass::moreTesting() called<br>";
}
}
?>

Calling parent constructors:


 Instead of writing an entirely new constructor for the subclass, let's write it by calling the parent's
constructor explicitly and then doing whatever is necessary in addition for instantiation of the
subclass. Here's a simple example:

class Name
{
var $_firstName;
var $_lastName;
function Name($first_name, $last_name)
{
$this->_firstName = $first_name;
$this->_lastName = $last_name;
}
function toString() {
return($this->_lastName .", " .$this->_firstName);
}
}
class NameSub1 extends Name
{
var $_middleInitial;
function NameSub1($first_name, $middle_initial, $last_name) {
Name::Name($first_name, $last_name);
$this->_middleInitial = $middle_initial;
}
function toString() {
return(Name::toString() . " " . $this->_middleInitial);
}
}

CHAPTER II

Department of Computer Science Page 14


WORKINGWITHFILES AND DIRECTORIES
READINGFILES
PHP’s file manipulation API is extremely flexible: it lets you read files into a string or into an arra y, from
the local file system or a remote URL, by lines, bytes, or characters.

Reading Local Files


The easiest way to read the contents of a disk file in PHPis with the file_get_ contents()function.
This function accepts the name and path to a disk file, and reads the entire file into a string variable in one
fell swoop.

Example:
<?php
//readfile intostring
$str= file_get_contents('example.txt') ordie('ERROR:Cannotfind
file');
echo$str;
?>
 An alternative method of reading data from a file is PHP’s file()function, which accepts the
name and path to a file and reads the entire file into an array, with each element of the array
representing one line of the file.
 To process the file, all you need do is iterate over the array using a foreachloop.
Example:
<?php
//readfile intoarray
$arr= file('example.txt') ordie('ERROR:Cannotfind
file');
foreach($arras$line) {
echo$line;
}
?>

Reading Remote Files


Both file_get_contents()and file()also support reading data from URLs, using either the HTTP
or FTP protocol, which reads an HTML file off the Web into an array:

Example:

Department of Computer Science Page 15


<?php
//readfile intoarray
$arr= file('http://www.google.com')ordie('ERROR:Cannotfindfile');
foreach($arras$line) {
echo$line;
}
?>
 In case of slow network links, it’s sometimes more efficient to read a remote file in “chunks,” to
maximize the efficiency of available network bandwidth.
 To do this, use the fgets()function to read a specific number of bytes from a file, as in the next
example:
<?php
//readfile intoarray(chunks)
$str= '';$fp= fopen('http://www.google.com','r')or
die('ERROR: Cannot openfile');
while(!feof($fp)) {
$str.=fgets($fp,512);
} fclose($fp);
echo$str;
?>
 First, the fopen()function: it accepts the name of the source file and an argument indicating
whether the file is to be opened in read ('r'), write ('w'), or append ('a') mode,and then creates
a pointer to the file.
 Next, a while loop calls the fgets()function continuously in a loop to read a specific number of
bytes from the file and append these bytes to a string variable; this loop continues until the
feof()function returns true, indicating that the end of the file has been reached.
 Once the loop has completed, the fclose()function destroys the file pointer.

WRITING FILES
 The flip side of reading data from files is writing data to them. And PHP comes with a couple of
different ways to do this as well.
 The first is the file_put_contents() function, a close cousin of the
file_get_contents()function you read about in the preceding section: this function accepts a
filename and path, together with the data to be written to the file,

Example:
Department of Computer Science Page 16
<?php
//writestringtofile
$data= "A fish \noutof\n water\n";
file_put_contents('output.txt', $data)or die('ERROR:Cannotwritefile');
echo'Datawritten tofile';
?>
Writing a file
 A new file can be written or text can be appended to an existing file using the PHP fwrite() function.
 This function requires two arguments specifying a file pointer and the string of data that is to be
written. Optionally a third integer argument can be included to specify the length of the data to write.
 If the third argument is included, writing would will stop after the specified length has been reached.
 The following example creates a new text file then writes a short text heading in site it. After
closingthisfileitsexistenceisconfirmedusingfile_exist()functionwhichtakesfilenameas an argument
Example:
<?php
$filename = "/home/user/guest/newfile.txt";
$file = fopen( $filename, "w" );
if( $file == false )
{
echo ( "Error in opening new file" );
exit();
}
fwrite( $file, "This isa simple test\n" );
fclose( $file );
?>
<html>
<head>
<title>Writing a file using PHP</title>
</head>
<body>
<?php
if(file_exist( $filename ) )
{
$filesize = filesize( $filename );
$msg = "File created with name $filename ";
Department of Computer Science Page 17
$msg .= "containing $filesize bytes";
echo ($msg );
}
else
{
echo ("File $filename does not exit" );
}
?>
</body>
</html>
PROCESSING DIRECTORIES
 PHP also allows developers to work with directories on the file system, iterating through directory
contents or moving forward and backward through directory trees.
 Iterating through a directory is a simple matter of calling PHP’s Directory Iterator object,

EXAMPLE

 Directory manipulation functions . . . as in the following listing:


<?php
//createdirectorypointer
$dp= opendir('.') ordie('ERROR:Cannot open directory');
//readdirectory contents
//print filenamesfound
while($file = readdir($dp)) {
if ($file!='.'&&$file !='..'){
echo"$file\n";
}
}
//destroydirectorypointer
closedir($dp);
?>
 Here, the opendir()function returns a pointer to the directory named in the function call.This
pointer is then used by the readdir()function to iterate over the directory, returning a single entry each
time it is invoked.
 It’s then easy to filter out the .and ..directories, and print the names of the remaining entries. Once
done, the closedir() function closes the file pointer.

Department of Computer Science Page 18


PHPFile and Director y Functions

Function What It Does


file_exists() Tests if a file or directory exists
filesize() Returns the size of a file in bytes
realpath() Returns the absolute path of a file
pathinfo() Returns an array of information about a file and its path
stat() Provides information on file attributes and permissions
is_readable() Tests if a file is readable
is_writable() Tests if a file is writable
is_executable() Tests if a file is executable
is_dir() Tests if a directory entry is a directory
is_file() Tests if a directory entry is a file
copy() Copies a file
rename() Renames a file
unlink() Deletes a file
mkdir() Creates a new directory
rmdir() Removes a directory
include()/require() Reads an external file into the current PHP script

Checking if a File or Director y Exists


 If you try reading or appending to a file that doesn’t exist, PHP will typically generate a fatal error.
 To avoid these error messages, always check that the file or directory you’re attempting to access
already exists, with PHP’s file_exists()function.

Example:

<?php
//checkfile
if(file_exists('somefile.txt')){
$str= file_get_contents('somefile.txt');
} else{
echo 'Namedfiledoesnotexist. ';
}

//checkdirectory
if(file_exists('somedir')){
$files= scandir('somedir');

Department of Computer Science Page 19


} else{
echo 'Nameddirectory doesnotexist.';
}
?>
Calculating File Size
 To calculate the size of a file in bytes, call the filesize()function with the filename as
argument:
Example:
<?php
//getfilesize
//output:'Fileis1327bytes.' if(file_exists('example.txt')){
echo'Fileis'.filesize('example.txt').'bytes.';
} else{
echo 'Namedfiledoesnotexist. ';
}
?>

Finding the Absolute File Path


 To retrieve the absolute file system path to a file, use the real path()function, as in the next
listing:
Example:
<?php
//getfile path
//output:'File path:/usr/local/apache/htdocs/
// /php-book/ch06/listings/example.txt'
if(file_exists('example.txt'))
{
echo'File path:'.realpath('example.txt');
} else
{
echo 'Namedfiledoesnotexist. ';
}
?>

Department of Computer Science Page 20


 You can also use the pathinfo()function, which returns an array containing the file’s path,
name, and extension. Here’s an example:

<?php
//getfile pathinfoasarray
if(file_exists('example.txt'))
{
print_r(pathinfo('example.txt'));
} else
{
echo 'Namedfiledoesnotexist. ';
}
?>
Retrieving File Attributes
 You can obtain detailed information on a particular file, including its ownership, permissions,
and modification and access times, with PHP’s stat()function, which returns this
information as an associative array. Here’s an example:

<?php
//getfile information
if(file_exists('example.txt'))
{
print_r(stat('example.txt'));
} else
{
echo 'Namedfiledoesnotexist. ';
}
?>

 You can check if a file is readable, writable or executable with the is readable (), is
writable (), and is executable () functions. The following example illustrates their
usage:

<?php
//getfile information
//output:'File is: readablewritable'

Department of Computer Science Page 21


if(file_exists('example.txt'))
{
echo'Fileis:';
//checkfor readablebit
if (is_readable('example.txt'))
{
echo'readable';
}
//checkfor writablebit
if (is_writable('example.txt'))
{
echo'writable';
}
//checkfor executablebit
if(is_executable('example.txt'))
{
echo'executable';
}
} else
{
echo 'Namedfiledoesnotexist. ';
}
?>

 The is_dir()function returns true if the argument passed to it is a directory, while the
is_file()function returns true if the argument passed to it is a file. Here’s an example:
<?php
//testiffileordirectory
if(file_exists('example.txt'))
{
if(is_file('example.txt'))
{
echo'It\'safile.';
}
if(is_dir('example.txt')){
Department of Computer Science Page 22
echo'It\'sadirectory.';
}
} else
{
echo 'ERROR: File doesnotexist.';
}
?>

Creating Directories
 To create a new, empty directory, call the mkdir()function with the path and name of the directory
to be created:
Example:
<?php
if(!file_exists('mydir'))
{
if(mkdir('mydir'))
{
echo'Directory successfully created.';
} else
{
echo 'ERROR: Directory couldnotbecreated.';
}
} else
{
echo 'ERROR: Directoryalreadyexists.';
}
?>
Copying Files
 You can copy a file from one location to another by calling PHP’s copy()function with the file’s
source and destination paths as arguments. Here’s an example:
<?php
//copyfile
if(file_exists('example.txt')){
if(copy('example.txt', 'example.new.txt')){
echo'File successfullycopied.';
Department of Computer Science Page 23
} else
{
echo 'ERROR: File couldnotbecopied.';
}
} else{
echo 'ERROR: File doesnotexist.';
}
?>
 It’s important to note that if the destination file already exists, the copy()function will overwrite it.

Renaming Files or Directories


 To rename or move a file (or directory), call PHP’s rename()function with the old and new path
names as arguments. Here’s an example that renames a file and a directory, moving the file to a
different location in the process:
Example:
<?php
//rename/movefile
if(file_exists('example.txt'))
{
if(rename('example.txt','../example.new.txt'))
{
echo'File successfullyrenamed.';
} else
{
echo 'ERROR: File couldnotberenamed.';
}
} else
{
echo 'ERROR: File doesnotexist.';
}
//renamedirectory
if(file_exists('mydir'))
{
if(rename('mydir','myotherdir'))
{

Department of Computer Science Page 24


echo'Directory successfully renamed.';
} else
{
echo 'ERROR: Directorycouldnotberenamed.';
}
} else
{
echo 'ERROR: Directory doesnotexist.';
}
?>
Reading and Evaluating External Files
 To read and evaluate external files from within your PHPscript, use PHP’s include() or
require()function.
 Avery common application of these functions is to include a standard header, footer, or copyright
notice across all the pages of aWeb site. Here’s an example:

<!DOCTYPEhtmlPUBLIC "-//W3C//DTD XHTML 1.0Transitional//EN"


"DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml"xml:lang="en"lang="en">
<head>
<title>MY WEB PAGE</title>
</head>
<body>
<?phprequire('header.php');?>
<p/>
Thisis thepagebody.
<p/>
<?phpinclude('footer.php');?>
</body>
</html>

Department of Computer Science Page 25


QUESTION BANK

UNIT IV

2 MARKS

1. Write short notes on user defined data types in php.


2. What are the elements of function?
3. Define class in php.
4. Define object on php.
5. Define inheritance.
6. What is object overloading?
7. Define data Abstraction.
8. Define encapsulation.
9. How will you create a class in php?
10. How will you create a object in php?
11. How calling member function works?
12. Define construction function.
13. Write short notes on public, protected, private members.
14. What is the use of the file_get_ contents()function?
15. What is the use of fopen(),fget(), fclose() functions?
16. List the php file directory functions.
17. What is the use of file_exist() function?
18. What is the use of file_size() function?
19. What is the use of realpath() function?
20. What is the use of stat() function?
21. What is the use of mkdir() function?
22. What is the use of copy() function?
23. What is the use of remove() function?
24. What is the use of require() and include() function?

5 MARKS AND 10 MARKS

1. Write brief notes on user defined functions in php.


2. Explain about oop concepts in php.
3. How will you read the files from file directory?
4. How will you write the files to file directory?
5. List php directory functions in php and explain it.

Department of Computer Science Page 26

You might also like