Word Press
Unit :1
Topics
By :Rathod Shukar
Points
1 : Class
2 : Property
3 : Visibility
4 : Constructor
5 : Destructor
6 : Inheritance
7 : Scope resolution(::)
8 : Autoloding Classes
9 : Class Constance
10 : Mysql(ins,upd,dlt..)
By :Rathod Shukar
 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
class structure
class <class-name>
{
<class body :-
Data Members & Methods>;
}
By :Rathod Shukar
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
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
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
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
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
Syntax
class classname
{
public function __construct()
{
//code
}
}
By :Rathod Shukar
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
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
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
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
< ?
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
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
 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
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
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
Example #1 Autoload example
<?php
spl_autoload_register(function ($class_name)
{
include $class_name . '.php';
});
$obj = new MyClass1();
$obj2 = new MyClass2();
?>
By :Rathod Shukar
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
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
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
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
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
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
Database Connection :
<?php
$con=mysql_connect("localhost","root","");
if($con)
{
echo"data base is conected....!!!!";
}
else
{
echo"data base is Not conected....!!!!";
}
?>
Mysql (insert,Update,Delete)
By :Rathod Shukar
Database Selection :
<?php
$db=mysql_select_db(“DatabaseName",$con);
if($db)
{
echo"data base is selected....!!!!";
}
else
{
echo"data base is Not selected....!!!!";
}
?>
By :Rathod Shukar
Insert :
Syntext :
$var=insert into tableName(field1, field1,…) Values(, , );
Ex :
<?php
$ins="insert into department(DeptNo,Name) values(1,’BCA’)“
?>
By :Rathod Shukar
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
Delete :
Syntext :
$var="delete from Table_Name where
Condition_Field=‘Value'";
<php
$del="delete from department where DeptNo='$del'";
?>
By :Rathod Shukar

Wordpress (class,property,visibility,const,destr,inheritence,mysql etc)

  • 1.
  • 2.
    Points 1 : Class 2: Property 3 : Visibility 4 : Constructor 5 : Destructor 6 : Inheritance 7 : Scope resolution(::) 8 : Autoloding Classes 9 : Class Constance 10 : Mysql(ins,upd,dlt..) By :Rathod Shukar
  • 3.
     A classis 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
  • 4.
    class structure class <class-name> { <classbody :- Data Members & Methods>; } 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 objectis 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 :  Classmember 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 :  Publicrefers 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 constructoris 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
  • 10.
    Syntax class classname { public function__construct() { //code } } By :Rathod Shukar
  • 11.
    class Customer { private $first_name; publicfunction __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 5introduces 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 ofthe 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 Nekudotayimwould, 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  Manydevelopers 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 Autoloadexample <?php spl_autoload_register(function ($class_name) { include $class_name . '.php'; }); $obj = new MyClass1(); $obj2 = new MyClass2(); ?> By :Rathod Shukar
  • 21.
    Class Constants  Itis 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 Definingand 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? MySQLis 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 AddressCity 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 isa 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 Connectto 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
  • 27.
    Database Connection : <?php $con=mysql_connect("localhost","root",""); if($con) { echo"database is conected....!!!!"; } else { echo"data base is Not conected....!!!!"; } ?> Mysql (insert,Update,Delete) By :Rathod Shukar
  • 28.
    Database Selection : <?php $db=mysql_select_db(“DatabaseName",$con); if($db) { echo"database is selected....!!!!"; } else { echo"data base is Not selected....!!!!"; } ?> By :Rathod Shukar
  • 29.
    Insert : Syntext : $var=insertinto tableName(field1, field1,…) Values(, , ); Ex : <?php $ins="insert into department(DeptNo,Name) values(1,’BCA’)“ ?> By :Rathod Shukar
  • 30.
    Update : Syntext : $var="updateTab_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="deletefrom Table_Name where Condition_Field=‘Value'"; <php $del="delete from department where DeptNo='$del'"; ?> By :Rathod Shukar