3. ďŽ A class is user defined data type that contains
attributes or data members; and methods which
work on the data members.
ďŽ To create a class, you need to use the keyword
class followed by the name of the class. The
name of the class should be meaningful to exist
within the system.
ďŽ The body of the class is placed between two curly
brackets within which you declare class data
members/variables and class methods.
Class :
By :Rathod Shukar
5. Example
class Customer {
private $first_name, $last_name;
public function getData($first_name, $last_name)
{
$this->first_name = $first_name;
$this->last_name = $last_name;
}
public function printData()
{
echo $this->first_name . " : " . $this->last_name;
}
}
By :Rathod Shukar
6. Object:-
ďŽ An object is a living instance of a class.
This means that an object is created from
the definition of the class and is loaded in
memory.
ďŽ Syntax :
ďŽ $obj_name = new ClassName();
By :Rathod Shukar
7. Properties :
ďŽ Class member variables are called "properties".
ďŽ You may also see them referred to using other
terms such as "attributes" or "fields", but for the
purposes of this reference we will use
"properties".
ďŽ They are defined by using one of the
keywords public, protected, or private, followed
by a normal variable declaration.
ďŽ This declaration may include an initialization,
but this initialization must be a constant value--
that is, it must be able to be evaluated at
compile time and must not depend on run-time
information in order to be evaluated
By :Rathod Shukar
8. Visibility :
ďŽ Public refers to methods and properties which
can be accessed or called from both within the
object itself and outside of the object (e.g. other
files and objects)
ďŽ Private refers to methods and properties which
can only be accessed or called from within the
object itself
ďŽ Protected refers to methods and properties
which can be accessed or called from within the
object itself and objects which extend from the
object (child objects) By :Rathod Shukar
9. Constructor:-
ďŽ A constructor is a special function of a class that is
automatically executed whenever an object of a class
gets instantiated.
ďŽ It is needed as it provides an opportunity for doing
necessary setup operations like initializing class
variables, opening database connections or socket
connections, etc. In simple terms, it is needed to setup
the object before it can be used.
ďŽ In PHP5 a constructor is defined by implementing the
__construct() method. This naming style has been
introduced in PHP5. In PHP4, the name of the
constructor was the same name as that of the class. So,
for example if you had a class Customer, you would
have to implement a function Customer().
By :Rathod Shukar
11. class Customer
{
private $first_name;
public function __construct()
{
$first_name = "";
$last_name = "";
$outstanding_amount = 0;
}
public function setData($first_name)
{
$this->first_name = $first_name;
}
public function printData() {
echo "Name : " . $first_name ;
}
}
$c1 = new Customer(); //object
$c1->setData("Sunilâ); //call class method
By :Rathod Shukar
12. Destructor:-
ďŽ PHP 5 introduces a destructor concept
similar to that of other object-oriented
languages, such as C++.
ďŽ The destructor method will be called as
soon as there are no other references to a
particular object, or in any order during the
shutdown sequence.
By :Rathod Shukar
13. Destructor Example
<?php
class MyDestructableClass
{
function __construct()
{
print "In constructorn";
$this->name = "MyDestructableClass";
}
function __destruct()
{
print "Destroying " . $this->name . "n";
}
}
$obj = new MyDestructableClass();
?> By :Rathod Shukar
14. Inheritance:-
ďŽ One of the main feature of Object Oriented
Programing which is called Inheritance. Basically
Inheritance is a mechanism where a new class
is derived from the existing base class.
ďŽ The derived class shares/inherit the functionality
of the base class. To extend the class behavior
PHP5 have introduced a new keyword called
âextendsâ.
ďŽ Note : Parent is the Keyword in PHP5, so donot
use in class, variable, object or method name.
By :Rathod Shukar
15. < ?
class parent1
{
protected $firstname = 11;
protected $lastname = 23;
}
class children extends parent1
{
function __construct()
{
echo $this->firstname;
echo $this->lastname;
}
}
$a = new children();
?>
By :Rathod Shukar
16. Scope Resolution Operator (::)
ďŽ The Scope Resolution Operator (also
called Paamayim Nekudotayim) or in
simpler terms, the double colon, is a token
that allows access to static, constant, and
overridden properties or methods of a
class.
ďŽ When referencing these items from
outside the class definition, use the name
of the class.
By :Rathod Shukar
17. ďŽ Paamayim Nekudotayim would, at first,
seem like a strange choice for naming a
double-colon. However, while writing the
Zend Engine 0.5 (which powers PHP 3),
that's what the Zend team decided to call
it. It actually does mean double-colon - in
Hebrew!
By :Rathod Shukar
18. Example #1 :: from outside the class definition
<?php
class MyClass
{
const CONST_VALUE = 'A constant value';
}
$classname = 'MyClass';
echo $classname::CONST_VALUE;
echo MyClass::CONST_VALUE;
?>
By :Rathod Shukar
19. Autoloading Classes
ďŽ Many developers writing object-oriented
applications create one PHP source file per
class definition.
ďŽ One of the biggest annoyances is having to
write a long list of needed includes at the
beginning of each script (one for each class).
ďŽ this is no longer necessary.
The spl_autoload_register() function registers any number of
autoloaders, enabling for classes and interfaces to be
automatically loaded if they are currently not defined. By
registering autoloaders, PHP is given a last chance to load the
class or interface before it fails with an error.
By :Rathod Shukar
20. Example #1 Autoload example
<?php
spl_autoload_register(function ($class_name)
{
include $class_name . '.php';
});
$obj = new MyClass1();
$obj2 = new MyClass2();
?>
By :Rathod Shukar
21. Class Constants
ďŽ It is possible to define constant values on a per-class basis
remaining the same and unchangeable. Constants differ from
normal variables in that you don't use the $ symbol to declare
or use them.
ďŽ The default visibility of class constants is public.
ďŽ The value must be a constant expression, not (for example) a
variable, a property, or a function call.
ďŽ It's also possible for interfaces to have constants. Look at
the interface documentation for examples.
ďŽ it's possible to reference the class using a variable. The
variable's value can not be a keyword
(e.g. self, parent and static).
ďŽ Note that class constants are allocated once per class, and not
for each class instance.
By :Rathod Shukar
22. Example #1 Defining and using a constant
<?php
class MyClass
{
const CONSTANT = 'constant value';
function showConstant() {
echo self::CONSTANT . "n";
}
}
echo MyClass::CONSTANT . "n";
$classname = "MyClass";
echo $classname::CONSTANT . "n";
$class = new MyClass();
$class->showConstant();
echo $class::CONSTANT."n";
?>
By :Rathod Shukar
23. What is MySQL?
MySQL is a database.
The data in MySQL is stored in database objects called tables.
A table is a collection of related data entries and it consists of
columns and rows.
Databases are useful when storing information categorically.
A company may have a database with the following tables:
"Employees", "Products", "Customers" and "Orders".
PHP MySQL Introduction`
MySQL is the most popular open-source database system.
By :Rathod Shukar
24. LastName FirstName Address City
Hansen Ola Timoteivn 10 Sandnes
Svendson Tove Borgvn 23 Sandnes
Pettersen Kari Storgt 20 Stavanger
Database Tables
A database most often contains one or more tables. Each table is identified
by a name (e.g. "Customers" or "Orders"). Tables contain records (rows)
with data.
Below is an example of a table called "Persons":
The table above contains three records
(one for each person) and four columns (LastName, FirstName,
Address, and City).
By :Rathod Shukar
25. Queries
A query is a question or a request.
With MySQL, we can query a database for specific information and
have a recordset returned.
SELECT LastName FROM Persons
:
LastName
Hansen
Svendson
Pettersen
The query above selects all the data in the "LastName" column from the "Persons" table
, and will return a recordset like this:
By :Rathod Shukar
26. PHP MySQL Connect to a Database`
Create a Connection to a MySQL Database
Before you can access data in a database, you must create a
connection to the database.
In PHP, this is done with the mysql_connect() function.
mysql_connect(servername,username,password);
Parameter Description
servername Optional. Specifies the server to connect to. Default value is
"localhost:3306"
username Optional. Specifies the username to log in with. Default value
is the name of the user that owns the server process
password Optional. Specifies the password to log in with. Default is ""
By :Rathod Shukar
29. Insert :
Syntext :
$var=insert into tableName(field1, field1,âŚ) Values(, , );
Ex :
<?php
$ins="insert into department(DeptNo,Name) values(1,âBCAâ)â
?>
By :Rathod Shukar
30. Update :
Syntext :
$var="update Tab_Name set field=âvalueâ where
Condition_Field=âValueââ;
<php
$upd="update Tab_Name set Name=âsritaâ where DeptNo=â1â;
?>
By :Rathod Shukar
31. Delete :
Syntext :
$var="delete from Table_Name where
Condition_Field=âValue'";
<php
$del="delete from department where DeptNo='$del'";
?>
By :Rathod Shukar