Encapsulation:
Encapsulation is the mechanism that binds together code and the data it
manipulates, and keeps both safe from outside interference and misuse. The
wrapping up of data and methods into a single unit (called class) is known as encapsulation.
The benefit of encapsulating is that it performs the task inside without making
you worry.
Polymorphism:
Objects could be of any type. A discrete object can have discrete properties
and methods which work separately to other objects. However a set of objects
could be derived from a parent object and retain some properties of the parent
class. This process is called polymorphism. An object could be morphed into
several other objects retaining some of its behavior.
Inheritance:
The key process of deriving a new object by extending another object is called
inheritance. When you inherit an object from another object, the subclass
(which inherits) derives all the properties and methods of the superclass
(which is inherited). A subclass can then process each method of superclass
anyway (which is called overriding).
Coupling:
Coupling is the behaviour of how classes are dependent on each other. Loosely
coupled architecture is much more reusable than tightly coupled objects. In the
next chapter we will learn details about coupling. Coupling is a very important
concern for designing better objects.
Design Patterns:
First invented by the "Gang of Four", design patterns are just tricks
in object oriented programming to solve similar sets of problems with a smarter
approach. Using design patterns (DP) can increase the performance of your whole
application with minimal code written by developers. Sometimes it is not
possible to design optimized solutions without using DP. But unnecessary and
unplanned use of DP can also degrade the performance of your application. We
have a chapter devoted for design patterns in this book.
Subclass:
A very common term in OOP, and we use this term throughout this book. When an
object is derived from another object, the derived one is called the subclass
of which it is derived from.
Superclass:
A class is superclass to an object if
that object is derived from it. To keep it simple, when you extend an object,
the object which you are extending is the superclass of a newly extended
object.
Instance: Whenever you create an
object by calling its constructor, it will be called an instance. To simplify
this, whenever you write some thing like this $var = new Object(); you
actually create an instance of object class.
Constructor:
A constructor method is the method that executes automatically
while creating instances of the class. In PHP5, there are two ways you can
write a constructor method inside a class. The first one is to create a method
with the name __construct()
inside the class. The second is to create a method naming exactly
the same as class name. For example if your class name is Emailer, the name of
the constructor method will be Emailer().
·
Notes: If you want to access any
method of the parent class (or you may say superclass) from which it is
derived, you can call using the parent keyword. For example, if you want to access a
method named sayHello,
you should write parent::sayHello();
·
If you declare any method as a final method,
it can't be overridden in any of its subclass.
·
Declare a class as final, which
will prevent anyone from extending it
Interface:
Interface is an empty class which contains only the declaration of methods. So
any class which implements this interface must contain the declared functions
in it. So, interface is nothing but a strict ruling, which helps to extend any
class and strictly implement all methods defined in interface. A class can use
any interface by using the implements keyword. Please note that in interface
you can only declare methods, but you cannot write their body. That means the
body of all methods must remain blank.
You can access a static method or property directly
without creating any instance of that class. A static member is like a global
member for that class and all instances of that class. Also, static properties
persist the last state of what it was assigned, which is very useful in some
cases.
Selecting
Data in an OO Way
Let's see how to select data from a table in an OO way
using MySQLi API.
<?php
$mysqli
= new mysqli("localhost", "un" "pwd",
"db");
if
(mysqli_connect_errno()) {
echo("Failed
to connect, the error message is : ".
mysqli_connect_error());
exit();}
/* close connection */
$result = $mysqli->query("select * from users");
while ($data = $result->fetch_object())
{
echo $data->name." : '".$data->pass."'
\n";
}
?>
The output is as following:
robin : 'no password'
tipu :
'bolajabena'
Create
a config file named config.php as in the following:
- <?php
- define("DB_HOST", 'localhost');
- define("DB_USER", 'root');
- define("DB_PASSWORD", '');
- define("DB_DATABSE", 'mypratice');
- ?>
Make
a database connection class. Create the file named dbConnect.php as in the
following:
- <?php
- class dbConnect {
- function __construct() {
- require_once('config.php');
- $conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
- mysql_select_db(DB_DATABSE, $conn);
- if(!$conn)// testing the connection
- {
- die ("Cannot connect to the database");
- }
- return $conn;
- }
- public function Close(){
- mysql_close();
- }
- }
- ?>
Make a
Function class. Create the file named dbFunction.php as in the following:
<?php
require_once
'dbConnect.php';
session_start();
class dbFunction {
function __construct() {
// connecting to database
$db = new dbConnect();;
}
// destructor
function __destruct() {
}
public function UserRegister($username,
$emailid, $password){
$password =
md5($password);
$qr = mysql_query("INSERT
INTO users(username, emailid, password)
values('".$username."','".$emailid."','".$password."')")
or die(mysql_error());
return $qr;
}
}
?>
After
the preceding procedure use the Function
include_once('dbFunction.php');
funObj = new dbFunction();
$register = $funObj->UserRegister($username,
$emailid, $password);
if($register){
echo
"<script>alert('Registration Successful')</script>";
}else{
echo
"<script>alert('Registration Not
Successful')</script>";
}
No comments:
Post a Comment