constructor and encapsulation in php

Post on 07-Apr-2017

200 Views

Category:

Engineering

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Web - Technology constructors and encapsulation in

php

B-Tech CSE 6th

13/NR/UT/CS005 Submitted by :- Shivani Soni

Submitted to :- Shivani Dhiman

PHP is an 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

PHP(Hypertext preprocessor)

PHP scripts reside between reserved PHP tags .This php tags allows the programmer to embed php scripts with in HTML pages.

Free Multi OS 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

Advantages and characteristics

PHP is also 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.

PHP as Object Oriented

Class-: classes are the "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.

Class and Object

<?php class pen{

} ?>

<?php Class house { 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

Example (How class and object will be declared)-:

Define:-Encapsulation is just 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.

Encapsulation in PHP

<?php

class App { private static $_user;

public function User( ) { if( $this->_user == null ) { $this->_user = new User(); } return $this->_user; }

}

Example

class User { private $_name;

public function __construct() { $this->_name = “Shivani"; }

public function GetName() { return $this->_name; } }

$app = new App();

echo $app->User()->GetName();

?>

If a class 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

Constructor in PHP

1)User Define Constructor

Note:-here we have called testA() method but we didn’t call A() method because it automatically called when object is initialized.

1)User Define Constructor

PHP introduce a 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.

2) Pre Defined Constructor

2) Pre Defined Constructor

Note:-

<?php class BaseClass { function __construct() { echo "Shivani\n"; } } class SubClass extends BaseClass { function __construct() { parent::__construct(); echo “soni\n"; } } $obj = new BaseClass();

$obj = new SubClass(); ?>

Example of constructors

top related