PHP: Databases and Classes. CS174. Chris Pollett. Sep 29, 2008.

Similar documents
PHP: Cookies, Sessions, Databases. CS174. Chris Pollett. Sep 24, 2008.

Database-Aware Fault Localization for Dynamic Web Applications

PHP. How Web Applications interact with server side databases CRUD. Connecting and using mysql from PHP PHP provides many mysql specific functions

COMP284 Scripting Languages Lecture 13: PHP (Part 5) Handouts

Chapter. Accessing MySQL Databases Using PHP

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

SQL stands for Structured Query Language. SQL lets you access and manipulate databases

Lecture 13: MySQL and PHP. Monday, March 26, 2018

Documentation for PHP ORMapper. version 2.0

Chapter 6 Part2: Manipulating MySQL Databases with PHP

CMSC 132: Object-Oriented Programming II

PHP Development - Introduction

Read this before starting!

More loops. Control structures / flow control. while loops. Loops / Iteration / doing things over and over and over and over...

Class, Variable, Constructor, Object, Method Questions

INHERITANCE. Spring 2019

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Server side scripting and databases

Remember our last lecture?

IELM 511 Information Systems Design Labs 5 and 6. DB creation and Population

Create Basic Databases and Integrate with a Website Lesson 3

Daniel Pittman October 17, 2011

ENCAPSULATION. private, public, scope and visibility rules. packages and package level access.

Read this before starting!

Jackson State University Department of Computer Science CSC / Advanced Information Security Spring 2013 Lab Project # 3

Chapter 5 Object-Oriented Programming

Databases and SQL. Lecture outline. CSE 190 M (Web Programming) Spring 2008 University of Washington

PHP. Main object oriented features (from php5 power programming)

Using PHP with MYSQL

13.1 Relational Databases (continued) 13.1 Relational Databases. - Logical model

13.1 Relational Databases

Perl (5 Days Content)

What is MySQL? [Document provides the fundamental operations of PHP-MySQL connectivity]

Midterm Exam CS 251, Intermediate Programming October 8, 2014

WEB APPLICATION ENGINEERING II

And Even More and More C++ Fundamentals of Computer Science

CS260 Intro to Java & Android 03.Java Language Basics

PHP Arrays. Lecture 18. Robb T. Koether. Hampden-Sydney College. Mon, Mar 4, 2013

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Programming II (CS300)

Clean Code in Small Companies

Networks and Web for Health Informatics (HINF 6220) Tutorial 13 : PHP 29 Oct 2015

CMSC 132: Object-Oriented Programming II

More on Objects in JAVA TM

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Programming II (CS300)

Principles of Object Oriented Programming. Lecture 4

Polymorphism 2/12/2018. Which statement is correct about overriding private methods in the super class?

Chapters 10 & 11 PHP AND MYSQL

ECE 122. Engineering Problem Solving with Java

CS 320 Introduction to Software Engineering Spring 2017

Extending Classes (contd.) (Chapter 15) Questions:

By the end of this section of the practical, the students should be able to:

Exceptions and Libraries

COM1004 Web and Internet Technology

A problem?... Exceptions. A problem?... A problem?... Suggested Reading: Bruce Eckel, Thinking in Java (Fourth Edition) Error Handling with Exceptions

CPSC 421 Database Management Systems. Lecture 10: Embedded SQL

Inheritance and Encapsulation. Amit Gupta

Development Technologies. Agenda: phpmyadmin 2/20/2016. phpmyadmin MySQLi. Before you can put your data into a table, that table should exist.

Today's Goals. CSCI 2910 Client/Server-Side Programming. Objects in PHP. Defining a Class. Creating a New PHP Object Instance

VIRTUAL FUNCTIONS Chapter 10

Programming for the Web with PHP

Compiling Java For High Performance on Servers

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Chapter 10 Classes Continued. Fundamentals of Java

Java: introduction to object-oriented features

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

CS 556 Distributed Systems

OCA Java SE 7 Programmer I Certification Guide By Mela Gupta. Arrays

More on Inheritance. Interfaces & Abstract Classes

CS-140 Fall 2017 Test 2 Version A Nov. 29, 2017

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Object-Oriented Modeling Using UML. CS151 Chris Pollett Aug. 29, 2005.

If you do not specify any custom parameters, we will deliver the message using the default names.

Objectives. Chapter 10. Developing Object-Oriented PHP. Introduction to Object-Oriented Programming

Final Exam CS 251, Intermediate Programming December 13, 2017

Mobile Application Programming. Swift Classes

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Inheritance, and Polymorphism.

The Basics. Concepts of Class and Object

PHP & My SQL Duration-4-6 Months

JAVA OBJECT-ORIENTED PROGRAMMING

Fundamentals of Object Oriented Programming

Lecture 18 CSE11 Fall 2013 Inheritance

PHP Arrays. Lecture 20. Robb T. Koether. Hampden-Sydney College. Wed, Feb 28, 2018

Professional Course in Web Designing & Development 5-6 Months

This lecture. PHP tags

Databases PHP I. (GF Royle, N Spadaccini ) PHP I 1 / 24

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

PHP Foundations. Rafael Corral, Lead Developer 'corephp' CMS Expo 2011

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

CS 11 java track: lecture 3

CS200: Advanced OO in Java

Systems Programming & Scripting

More On inheritance. What you can do in subclass regarding methods:

Lecture 21: The Many Hats of Scala: OOP 10:00 AM, Mar 14, 2018

Records. ADTs. Objects as Records. Objects as ADTs. Objects CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 15: Objects 25 Feb 05

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

Definition of DJ (Diminished Java)

Transcription:

PHP: Databases and Classes. CS174. Chris Pollett. Sep 29, 2008.

Outline. Databases. Classes.

Connecting to MySQL from PHP. To start a connect to a MySQL database one can issue the command: $db = mysql_connect(); This function actually takes three parameters: the host, the username, and the password. These default to localhost, the process name PHP runs under, and blank. $db = mysql_connect(host, uname,pword); Depending on how mysql is configured, the first example above might work and saves some typing. This function returns false if a connection is not made. To close a database, one can call mysql_close();

Selecting a Database and queries. To select a database one calls: mysql_select_db( cars ); One can then do a query with a command like: $query = SELECT * FROM Corvettes ; $result = mysql_query($query); $num_rows = mysql_num_rows($result); $num_fields = mysql_num_fields($result); for($j =1; $j <=$num_rows; $j++) { $row = mysql_fetch_array($result); print $row[0].$row[ some_attr ]. <br /> ; } mysql_query can also be used to do inserts, etc.

Classes in PHP. Classes in PHP are in many ways similar to classes in Java. To define a class one uses the keyword class as in: class MyFirstClass { var $myvariable = 0; function getmyvariable() { return $this->myvariable; //note need to use $this. } } To create an instance of a class and invoke methods I can then use: var $myclass = new MyFirstClass(); echo My 1st var: {$myclass->getmyvariable()} ;

Including Classes. Typically, you put the code for your class into a file and then use a line like: require( MyClass.inc ); //or more likely. require_once( MyClass.inc ); The require function is similar to include except when it fails it gives a fatal error rather than a warning. Also you cannot use require to include remote files even if allow_url_fopen is enabled. require_once will not re-include the file if it has already been included.

Constructors/Destructors. A couple slides back we saw we could set up an initial value of a field variable of a class when we declare it: var $myvariable = 0; You can also have a functions construct and destruct to do initialization and clean-up. function construct($n=0) { $this->myvariable = $n; }

Private, Protected, Public. Member variables and member functions can be declared private, protected or public: private var $myfield; protected function mymethod() { /* some code*/}. Methods without any declaration are the same as public. Private means only visible within the class. Protected is visible within the class or within subclasses. Public means variable or method is visible to anyone.

Static and Const. The static keyword creates one instance of the field or method for the object. class Foo{ static $bob=1;} echo bob: {Foo::$bob} ; Within the class use self:: to refer to static members. The const keyword can be used to define constants for a class: class Goo{ const blob=1;} Notice no dollar sign. Can refer to this using Goo::blob. Values of constants cannot be changed.

Cloning. PHP has a command clone for cloning objects: $my_copy = clone $my_obj; To specify how the copying is done you can write a clone() method for your class.

Inheritance. Inheritance in PHP is very similar to Java. A PHP class can extend one other class. class A{} class B extends A {} PHP also has a notion of interface: interface myinterface. { function method1($a, $b); } A PHP class can implement multiple interfaces: class C implements myinterfacea, myinterfaceb {}. If you want to have a class with some but not all of its methods defined. You can use the keyword abstract on those methods which will be overriden in subclasses.

Referring to Parents, Final. Consider: 1. class A {function foo(){} }. 2. class B extends A {function foo(){} }. Within B this::foo() refers to the redefinition of foo given in (2). Within B parent::foo() refers to class A s version of foo. To prevent a function from being overridden in a subclass you can use the keyword final. class A {final function foo() {} /* can t override*/ }.

Exceptions. PHP supports try catch blocks like Java. try {} catch(myexception $e){} catch(exception $ee){}. You can use the keyword throw to throw an exception. if($denom == 0){ throw new Exception( divide by zero ); } You can subclass Exception to create custom exceptions.