KEMBAR78
Constructor and encapsulation in php | PPTX
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 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
 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.
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.
<?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
 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.
 <?php
 class App {
 private static $_user;
 public function User( ) {
 if( $this->_user == null ) {
 $this->_user = new User();
 }
 return $this->_user;
 }
 }
 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
 Note:-here we have called testA() method but
we didn’t call A() method because it
automatically called when object is initialized.
 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.
 <?php
 class BaseClass {
 function __construct()
 {
 echo "Shivanin";
 }
 }
 class SubClass extends BaseClass
 {
 function __construct()
 {
 parent::__construct();
 echo “sonin";
 }
 }
 $obj = new BaseClass();
 $obj = new SubClass();
 ?>
Constructor and encapsulation in php
Constructor and encapsulation in php

Constructor and encapsulation in php

  • 1.
    B-Tech CSE 6th 13/NR/UT/CS005 Submittedby :- Shivani Soni Submitted to :- Shivani Dhiman
  • 2.
     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.
  • 9.
     <?php  classApp {  private static $_user;  public function User( ) {  if( $this->_user == null ) {  $this->_user = new User();  }  return $this->_user;  }  }
  • 10.
     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();  ?>