This document discusses PHP, an open-source server-side scripting language. It describes PHP's origins and uses, advantages like being free, multi-OS supported, easy to understand and use, and compatible with many databases. It also discusses PHP's object-oriented capabilities like classes, objects, encapsulation, inheritance via extending classes, and constructors. Examples are provided to illustrate these OOP concepts in PHP code.
PHP isan open source , server-side scripting
language.
PHP is used to generate dynamic web pages.
Originally created by Rasmus Lerdorf in 1994,
the PHP reference implementation is now
produced by The PHP Group.PHP originally
stood for Personal Home Page , but it now
stands for Hypertext Preprocessor
3.
PHP scripts residebetween reserved PHP tags .
This php tags allows the programmer to embed
php scripts with in HTML pages.
4.
Free
MultiOS Support(like linux,windows,OSX)
Easier to fix problem
Easy to understand
Speed
Object oriented
Compatible with many databases(SQL,MySQL)
Simple efficient
Secure flexible
familiar
5.
PHP isalso OOP language as it also
based on the concepts of object and
classes. PHP also uses the object
oriented concepts like encapsulation ,
inheritance , polymorphism , data
abstraction and constructors.
6.
Class-: classes arethe "blueprints“ for an
object and the actual code that defines the
properties and methods . and the class is
declared with in the tags.
Objects-: An object is an instance
of a class.
Any number of instances
of a class can be created.
<?php
class pen
{
}
?>
7.
<?php
Classhouse
{
Public $name=“Soni”;
Public $age=47;
Function hello()
{
Echo “Hello world”;
}
$h=new house();
$g=new house();
Echo $h->name . “is” .$h->age .”year old”;
Echo $g->hello ()
?>
The output is-:soni is 47 year old hello world
8.
Define:-Encapsulation isjust wrapping of data in single
unit also we can say hiding the information of essential
details . Or we can say that it is the process of hiding the data
of the object from outside world and accessed to it is
restricted to members of the class.
Example:- Let You have a mobile phone…there it some
interface which help You to interact with cell phone and you
can uses the services of mobile phone. But the actually
working in cell phone is hide. You don’t know how it works
internally.
class User{
private $_name;
public function __construct() {
$this->_name = “Shivani";
}
public function GetName() {
return $this->_name;
}
}
$app = new App();
echo $app->User()->GetName();
?>
11.
If aclass name and function name will be
similar in that case function is known as
constructor. constructor is special type of
method because its name is similar to class
name . constructor automatically calls when
object will be initializing.
There are two types of constructor
1) user defined constructor
2) predefined constructor
13.
Note:-here wehave called testA() method but
we didn’t call A() method because it
automatically called when object is initialized.
14.
PHP introducea new functionality to
define a constructor i.e __construct().
By using this function we can define a
constructor. it is known as predefined
constructor.
__construct() are also known as magic
function in PHP.
17.
<?php
classBaseClass {
function __construct()
{
echo "Shivanin";
}
}
class SubClass extends BaseClass
{
function __construct()
{
parent::__construct();
echo “sonin";
}
}
$obj = new BaseClass();
$obj = new SubClass();
?>