+ All Categories
Home > Engineering > Constructor and encapsulation in php

Constructor and encapsulation in php

Date post: 07-Apr-2017
Category:
Upload: shivani-soni
View: 200 times
Download: 2 times
Share this document with a friend
19
Web - Technology constructors and encapsulation in php B-Tech CSE 6 th 13/NR/UT/CS005 Submitted by :- Shivani Soni Submitted to :- Shivani Dhiman
Transcript
Page 1: Constructor and encapsulation in php

Web - Technology constructors and encapsulation in

php

B-Tech CSE 6th

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

Submitted to :- Shivani Dhiman

Page 2: Constructor and encapsulation in php

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)

Page 3: Constructor and encapsulation in php

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

Page 4: Constructor and encapsulation in php

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

Page 5: Constructor and encapsulation in php

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

Page 6: Constructor and encapsulation in php

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{

} ?>

Page 7: Constructor and encapsulation in php

<?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)-:

Page 8: Constructor and encapsulation in php

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

Page 9: Constructor and encapsulation in php

<?php

class App { private static $_user;

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

}

Example

Page 10: Constructor and encapsulation in php

class User { private $_name;

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

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

$app = new App();

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

?>

Page 11: Constructor and encapsulation in php

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

Page 12: Constructor and encapsulation in php

1)User Define Constructor

Page 13: Constructor and encapsulation in php

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

Page 14: Constructor and encapsulation in php

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

Page 15: Constructor and encapsulation in php

2) Pre Defined Constructor

Page 16: Constructor and encapsulation in php

Note:-

Page 17: Constructor and encapsulation in php

<?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

Page 18: Constructor and encapsulation in php
Page 19: Constructor and encapsulation in php

Recommended