15 – PHP(5) Informatics Department Parahyangan Catholic University.

Slides:



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

UFCE8V-20-3 Information Systems Development 3 (SHAPE HK) Lecture 3 PHP (2) : Functions, User Defined Functions & Environment Variables.
Python Objects and Classes
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Chapter 7: User-Defined Functions II
The Web Warrior Guide to Web Design Technologies
Object-Oriented PHP (1)
1 Lab Session-XIV CSIT121 Spring 2002 b Namespaces b First Class Travel b Lab Exercise 14 (Demo) b Lab Exercise b Practice Problem.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Guide To UNIX Using Linux Third Edition
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Review of C++ Programming Part II Sheng-Fang Huang.
Python 3 Some material adapted from Upenn cis391 slides and other sources.
CS0007: Introduction to Computer Programming File IO and Recursion.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Chapter 8 More Object Concepts
Data Objects (revisited) Recall that values are stored in data objects, and that each data object holds one value of a particular type. Data objects may.
1 Understanding Inheritance COSC 156 C++ Programming Lecture 8.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
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.
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.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Storing and Retrieving Data
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Class Builder Tutorial Presented By- Amit Singh & Sylendra Prasad.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Pointers *, &, array similarities, functions, sizeof.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
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,
PHP-5- Working with Files and Directories. Reading Files PHP’s file manipulation API is extremely flexible: it lets you read files into a string or into.
Chapter 3 Introduction to PHP. Incorporating PHP Within HTML By default, PHP documents end with the extension.php files ending with.htm or.html to also.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
FILES. open() The open() function takes a filename and path as input and returns a file object. file object = open(file_name [, access_mode][, buffering])
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
FIT Objectives By the end of this lecture, students should: understand the role of constructors understand how non-default constructors are.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Today Encapsulation. Build a fully encapsulated Halloween class, going from Halloween1 to Halloween6 (eventually!): –The final version will have overloaded.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Object-Oriented Concepts
Programming Language Concepts (CIS 635)
Web Systems Development (CSC-215)
METHODS AND BEHAVIORS AKEEL AHMED.
CSC 113 Tutorial QUIZ I.
Java Programming Language
CMSC 202 Java Primer 2.
Java Inheritance.
Object-Oriented Programming in PHP
CIS 199 Final Review.
Classes and Objects Imran Rashid CTO at ManiWeber Technologies.
ITM 352 Functions.
Presentation transcript:

15 – PHP(5) Informatics Department Parahyangan Catholic University

 Sometimes we need to work with zip files, for example:  compressing several files to be downloaded at once  uncompressing a zip file that the user uploaded  etc.  PHP version 5.2 and later comes with ZipArchive class

 To create a new archive, first create an object of ZipArchive class:  Then we open the new archive with the open() method: $zip = new ZipArchive(); $filename = "abc.zip"; if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) { exit("cannot open $filename\n"); }

 There are 4 modes available when opening an archive:  ZipArchive::OVERWRITE Always start a new archive, this mode will overwrite the file if it already exists.  ZipArchive::CREATE Opens an existing zip file, or create a new archive if it does not exist.  ZipArchive::EXCL Error if archive already exists.  ZipArchive::CHECKCONS Perform additional consistency checks on the archive, and error if they fail (eg. broken zip, corrupted upload, etc.)

 The addFile() method adds a file to an opened archive  The close() method closes the active archive and saves changes $zip->addFile("Document1.txt"); $zip->addFile("Document2.txt"); $zip->addFile("Document3.txt"); $zip->close();

 The addFile() method can give a new name for the added file by providing the second argument  Example: $zip->addFile("Document1.txt", "File 1.txt"); $zip->addFile("Document2.txt", "File 2.txt"); $zip->addFile("Document3.txt", "File 3.txt");

 Several useful attributes/methods:  numFiles attribute the number of files contained in the active archive file  filename attribute the complete path of the active archive file  statIndex() method Get the details of an entry defined by its index  statName() method Get the details of an entry defined by its name  setPassword() method Set a password for opening an archive

 Example: $zip = new ZipArchive(); $filename = "abc.zip"; if ($zip->open($filename) != TRUE) { exit("cannot open \n"); } echo "numFiles: ". $zip->numFiles. " "; echo "filename: ". $zip->filename. " "; for ($i=0; $i numFiles;$i++) { $current = $zip->statIndex($i); echo "index $i : "; printf("%s (size: %d byte(s)) ", $current["name"], $current["size"]); } when open() is used without the second parameter, it tries to open an existing zip file, returning FALSE when the file doesn’t exist

 Example:

 The extractTo() method extracts the complete archive or the given files to the specified destination.  Syntax: bool extractTo(string $destination [, mixed $entries]) single entry name or array of names

 Example:  extracts the complete archive  extracts one file  extracts an array of files $destination = "extracted/abc"; $zip->extractTo($destination); $destination = "extracted/abc"; $zip->extractTo($destination, "Document1.txt"); $destination = "extracted/abc"; $zip->extractTo($destination, array("Document1.txt", "Document2.txt"));

 The addEmptyDir() method adds an empty directory in the archive  Example: $zip->addEmptyDir("newDir1"); $zip->addFile("Document1.txt", "newDir1/Document1.txt"); $zip->addEmptyDir("newDir2"); $zip->addFile("Document2.txt", "newDir2/Document2.txt"); Unix / Windows Compatibility When specifying a path on Unix platforms, a forward slash (/) is used as directory separator. On Windows platforms, both forward slash (/) and backslash (\) can be used. Unix / Windows Compatibility When specifying a path on Unix platforms, a forward slash (/) is used as directory separator. On Windows platforms, both forward slash (/) and backslash (\) can be used.

 The Directory class allow us to retrieve information about directories and their contents.  Instances of Directory are created by calling the dir() function, not by the new operator.  The getcwd() function returns a string containing the current working directory.

 The read() method returns the name of the next entry in the directory. The entries are returned in the order in which they are stored by the file system.  The close() method closes the previously opened directory handle

 Example: $d = dir(getcwd()); echo "Path: ". $d->path. " "; while (($file = $d->read()) !== false){ echo "filename: ". $file. " "; } $d->close();

 Example:

 Example: processing.txt files only $d = dir(getcwd()); echo "Path: ". $d->path. " "; while (($file = $d->read()) !== false){ if(pathinfo($file, PATHINFO_EXTENSION) == "txt") echo $file. " "; } $d->close();

 The is_dir() function tells whether the given filename is a directory  Example: processing directory only $d = dir(getcwd()); echo "Path: ". $d->path. " "; while (($file = $d->read()) !== false){ if(is_dir($file)) echo $file. " "; } $d->close();

 The addFile() method adds a single file to a zip archive. To zip an entire directory, we need to recursively iterate all the directories and adds all the files one by one.  One implementation by Ramui can be found here: directory.html directory.html

 Class definitions contain the class name (which is case-sensitive), its attributes, and its methods.  Example: class User{ public $name; public $password; function User($param1, $param2){ $this->name=$param1; $this->password=$param2; } function save_user(){ echo "Save User code goes here"; }

 To create an object with a specified class, use the new keyword  Example:  $object = new User;  $temp = new User('name', 'password');

 To access object’s attribute: $object->attribute  To access object’s method: $object->method()  The property and method names do not have dollar signs ( $ ) in front of them.

 Once you have created an object, it is passed by reference when you pass it as a parameter.  In other words, making object assignments does not copy objects in their entirety.  Example: this prints “Wombaty” $u1 = new User("Wombat", "womwom"); $u2 = $u1; $u1->name = "Wombaty"; echo $u2->name;

 The clone operator creates a new instance of the class and copies the attribute values from the original instance to the new instance.  Example: This prints “Wombat” $u1 = new User("Wombat", "womwom"); $u2 = clone $u1; $u1->name = "Wombaty"; echo $u2->name;

 Starting from PHP5, a method can be declared as static, which means that it is called on a class and not on an object  To call a static method, write the class’ name, followed by a double colon (::), followed by the method’s name

 Example: class User{ public $name; public $password; function User($param1, $param2){ $this->name=$param1; $this->password=$param2; } static function pwd_string(){ echo "Please enter your password"; } User::pwd_string();

 An attribute can be given a default value  It must be a constant and not the result of a function or expression  Example: class User{ public $name = "John Smith"; //valid public $password = time();//not valid }

 A constant is declared using const keyword. Its value, once declared, cannot be changed.  A constant’s name is usually written in all UPPERCASE letter.  Constants can be referenced directly, using the self keyword and the double colon operator (::)

 Example: Translate::lookup(); class Translate{ const ENGLISH = 0; const SPANISH = 1; const FRENCH = 2; const GERMAN = 3; //... static function lookup(){ echo self::SPANISH; }

 PHP 5 provides three keywords for controlling the scope of attributes and methods: public, protected, private.  The default scope is public

 public  can be referenced from anywhere  protected  can be referenced only by the object’s class methods and those of any subclasses.  private  can be referenced only by methods within the same class—not by subclasses.

 Once we have written a class, we can derive subclasses from it.  This is achieved using the extends operator. class Subscriber extends User{ public $phone, $ ; function display(){ echo "Name: ". $this->name. " "; echo "Pass: ". $this->password. " "; echo "Phone: ". $this->phone. " "; echo " ". $this-> ; }

 The parent keyword is used to access the superclass’ attribute/methods  Example: accessing parent’s constructor class Subscriber extends User{ public $phone, $ ; function Subscriber($p1, $p2, $p3, $p4){ parent::User($p1, $p2); $this->phone = $p3; $this-> = $p4; } When extending a class, PHP does not automatically calls the parent class’ constructor

 If you write a method in a subclass with the same name as one in its parent class, its statements will override those of the parent class.  However, the parent class’ method is still accessible by using parent keyword.

 In cases in which we wish to prevent a subclass from overriding a superclass’ method, we can use the final keyword.  Example: class User{ final function copyright(){ echo "This class was written by Joe Smith"; }