Php notes, Lecture notes for Computer Engineering and Programming. Tribhuvan University Kathmandu
saud.prakash
saud.prakash27 January 2016

Php notes, Lecture notes for Computer Engineering and Programming. Tribhuvan University Kathmandu

DOCX (44 KB)
12 pages
664Number of visits
Description
PHP notes related to joins, class
20 points
Download points needed to download
this document
Download the document
Preview3 pages / 12

This is only a preview

3 shown on 12 pages

Download the document

This is only a preview

3 shown on 12 pages

Download the document

This is only a preview

3 shown on 12 pages

Download the document

This is only a preview

3 shown on 12 pages

Download the document

Abstract Classes and Interface in PHP Abstract class and Interface in php play very important role in oop. In this section we will discuss following point

1. What is abstract classes. 2. What is interface 3. How to implement abstract classes in php

4. How to implement interface in php 5. Different between abstract classes and interface.

What is abstract Classes As from name it seem like something that is hidden. Yes nature of the abstract classes are same. Abstract classes are those classes which can not be directly initialized. Or in other word we can say that you can not create object of abstract classes. Abstract classes always created for inheritance purpose. You can only inherit abstract class in your child class. Lots of people say that in abstract class at least your one method should be abstract. Abstract method are the method which is only defined but declared. This is not true definition as per my assumption. But your any class has at least one method abstract than your class is abstract class.

Usually abstract class are also known as base class. We call it base class because abstract class are not the class which is available directly for creating object. It can only act as parent class of any normal class. You can use abstract class in class hierarchy. Mean one abstract class can inherit another abstract class also.

Abstract classes in PHP Abstract classes in php are simillar like other oop languages. You can create abstract classes in php using abstract keyword. Once you will make any class abstract in php you can not create object of that class. abstract class abc { public function xyz() { return 1; } }

$a = new abc();//this will throw error in php

above code will throw error in php.

Abstract classes in php are only for inheriting in other class. abstract class testParent { public function abc() { //body of your funciton } } class testChild extends testParent { public function xyz() { //body of your function } }

$a = new testChild();

In above example you are creating of testChild Class. TestChild class is inheriting testParent abstract class. So your abstract class is only available for inheritance. Main motive of creating abstract classes in php is to apply restriction of direct initialization or object creation.

Implementation of abstract method As we know that abstract functions are those functions of abstract class which is only defined. It will be declared in your child class. You can create any method abstract using keyword abstract. You can only create abstract method either in abstract class or interface. Following is example of the abstract method implementation:

abstract class abc { abstract protected function f1($a , $b); } class xyz extends abc { protected function f1($name , $address) { echo "$name , $address"; } } $a = new xyz();

In class abc we have defined an abstract function f1. Now when we have inherited class abc then declared function f1. If you have an abstract method in your abstract class then once you inherit your abstract class then it is necessary to declare your abstract method. If you will not declare your abstract method then PHP will throw error in that case.

You can declare your abstract method in child class with the same visibility or less restricted visibility.

abstract class parentTest { abstract protected function f1(); abstract public function f2(); //abstract private function f3(); //this will trhow error } class childTest { public function f1() { //body of your function } public function f2() { //body of your function } protected function f3() { //body of your function } } $a = new childTest();

In above code you can see that you have declare 3 function in abstract class. But private declaration of the abstract method will always throw error. Because private method is availabe only in the same class context. But in case of f1. This is protected. Now in child class we have defined it as public because public is less restricted than protected. And for function f2 which is already public so we have defined it as public in our child class. We have defined it public because no any visibility is less restricted than public.

What is Interface ?

Interface in oop enforce definition of some set of method in the class. By implementing interface you are forcing any class to must declaring some specific set of methods in oop. For example if you are creating class to render HTML element then it is necessary to set id and name of your html tag. So in this case you will create interface for that class and define method like setID and setName. So whenever someone will create any class to render HTML tag and implemented your interface then he must need to define setId and setName method in their class. In other word you can say that by help of interface you can set some definition of your object. Interface is very useful if you are creating architecture of any oop base application. Inter

Interface in PHP Interface in php can be implemented like other oop lanugage. You can create interface in php using keyword interface. By implementation of interface in php class you are specifying set of the method which classes must implement.

You can create interface in php using interface keyword. Rest of the things are typically identical to classes. Following is very small example of interface in php. interface abc { public function xyz($b); }

So in above code you are creating interface with name abc. Interface abc has function xyz. Whenever you will implement abc interface in your class then you have to create method with name xyz. If you will not create function xyz then it will throw error.

You can implement your interface in your class using implements keyword. Let us implement our interface abc in our class class test implements abc { public function xyz($b) { //your function body } }

You can only define method in interface with public accessibility. If you will use other than public visibility in interface then it will throw error. Also while defining method in your interface do not use abstract keyword in your methods.

You can also extend interface like class. You can extend interface in php using extends keyword.

interface template1 { public function f1(); } interface template2 extends template1 { public function f2(); } class abc implements template2 { public function f1() { //Your function body } public function f2() { //your function body }

}

So here template2 has all property of tempate2. So whenever you will implement template2 in your class, you have to create function of both interfaces.

You can also extend multiple interface in one interface in php. interface template1 { public function f1(); } interface template2 { public function f2(); } interface template3 extends template1, template2 { public function f3(); } class test implements template3 { public function f1() { //your function body } public function f2() { //your function body } public function f3()

{ //your function body } }

You can also implement more than one interface in php class. interface template1 { public function f1(); } interface template2 { public function f2(); } class test implments template1, template2 { public function f1() { //your function body } public function f2() { //your function body }

}

You can not implement 2 interfaces if both share function with same name. It will throw error.

Your function parameter in class must be identical to the parameter in the interface signature. Following is example some example interface template1 { public function f1($a) } class test implements template1 { public function f1($a) { echo $a; }

}

Above will work. But following example will not work: interface template1 { public function f1($a)

} class test implements template1 { public function f1() { echo $a; }

}

But it is not necessary to use the same name of the variable. Like $a. You can also use any name. For example: interface template1 { public function f1($a) } class test implements template1 { public function f1($name) { echo $name; }

}

If you are using default argument then you can change your value of the argument. For example interface template1 { public function f1($a = 20) } class test implements template1 { public function f1($name = "ankur") { echo $name; } }

In above section we have discussed interfaces and abstract classes in php. Both are almost doing same things but has some difference.

Differences between abstract class and interface in PHP Following are some main difference between abstract classes and interface in php

1. In abstract classes this is not necessary that every method should be abstract. But in interface every method is abstract.

2. Multiple and multilevel both type of inheritance is possible in interface. But single and multilevel inheritance is possible in abstract classes.

3. Method of php interface must be public only. Method in abstract class in php could be public or protected both.

4. In abstract class you can define as well as declare methods. But in interface you can only defined your methods.

Joins CREATE TABLE `user` (`id` smallint(5) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(30) NOT NULL,`course` smallint(5) unsigned DEFAULT NULL, PRIMARY KEY (`id`) )

id name course 1 Alice 1 2 Bob 1 3 Caroline 2 4 David 5 5 Emma (NULL)

CREATE TABLE `course` ( `id` smallint(5) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(50) NOT NULL,PRIMARY KEY (`id`) )

id name 1 HTML5

2 CSS3 3 JavaScript

4 PHP

5 MySQL

1: INNER JOIN:

The INNER JOIN selects all rows from both tables as long as there is a match between the columns in both tables.

Syntax SELECT column_name(s) FROM table1 INNER JOIN table2 ON table1.column_name=table2.column_name;

Example:

This produces a set of records which match in both the user and course tables, i.e. all users who are enrolled on a course.

SELECT user.name, course.name FROM `user` INNER JOIN `course` on user.course = course.id;

user.name course.name Alice HTML5 Bob HTML5

Carline CSS3

David MySQL

2: LEFT JOIN: The LEFT JOIN keyword returns all rows from the left table (table1), with the matching rows in the right table (table2). The result is NULL in the right side when there is no match.

Syntax SELECT column_name(s) FROM table1 LEFT JOIN table2 ON table1.column_name=table2.column_name;

Example

What if we require a list of all students and their courses even if they’re not enrolled on one? A LEFT JOIN produces a set of records which matches every entry in the left table (user) regardless of any matching entry in the right table (course):

SELECT user.name, course.name FROM `user` LEFT JOIN `course` on user.course = course.id;

user.name course.name Alice HTML5 Bob HTML5

Carline CSS3 David MySQL

Emma (NULL)

3: RIGHT JOIN:

The RIGHT JOIN keyword returns all rows from the right table (table2), with the matching rows in the left table (table1). The result is NULL in the left side when there is no match.

Syntax SELECT column_name(s) FROM table1 RIGHT JOIN table2 ON table1.column_name=table2.column_name;

Example:

Perhaps we require a list all courses and students even if no one has been enrolled? A RIGHT JOIN produces a set of records which matches every entry in the right table (course) regardless of any matching entry in the left table (user):

SELECT user.name, course.name

FROM `user`

RIGHT JOIN `course` on user.course = course.id;

user.name course.name Alice HTML5

Bob HTML5 Carline CSS3 (NULL) JavaScript (NULL) PHP

David MySQL

4: OUTER JOIN (or FULL OUTER JOIN) The FULL OUTER JOIN keyword returns all rows from the left table (table1) and from the right table (table2). The FULL OUTER JOIN keyword combines the result of both LEFT and RIGHT joins.

Syntax SELECT column_name(s) FROM table1

FULL OUTER JOIN table2 ON table1.column_name=table2.column_name;

Example:

Our last option is the OUTER JOIN which returns all records in both tables regardless of any match. Where no match exists, the missing side will contain NULL. OUTER JOIN is less useful than INNER, LEFT or RIGHT and it’s not implemented in MySQL. However, you can work around this restriction using the UNION of a LEFT and RIGHT JOIN, e.g.

SELECT user.name, course.name FROM `user` LEFT JOIN `course` on user.course = course.id UNION SELECT user.name, course.name FROM `user` RIGHT JOIN `course` on user.course = course.id;

user.name course.name Alice HTML5 Bob HTML5

Carline CSS3 David MySQL

Emma (NULL) (NULL) JavaScript (NULL) PHP

comments (0)

no comments were posted

be the one to write the first!

This is only a preview

3 shown on 12 pages

Download the document