SHARE
TWEET

Object Oriented PHP (Magic Method -toString)

bappy7706 Jun 1st, 2020 1,172 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport"
  6.           content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  7.     <meta http-equiv="X-UA-Compatible" content="ie=edge">
  8.     <title>Document</title>
  9. </head>
  10. <body>
  11. <h1 style="text-align: center;color: maroon;font-weight: bold; font-family: 'Mongolian Baiti'"> Object Oriented PHP (Magic Method ======  __toString())</h1>
  12. <br>
  13.  
  14. <?php
  15.  
  16.  
  17.  
  18.  class Student
  19. {
  20.  
  21.     protected $name;
  22.     protected $age;
  23.  
  24.     public function getN()
  25.     {
  26.         return $this->name;
  27.  
  28.     }
  29.  
  30.  
  31.      public function getA()
  32.      {
  33.          return $this->age;
  34.  
  35.      }
  36.  
  37.      private function setN($name)
  38.      {
  39.          $this->name=$name;
  40.  
  41.      }
  42.  
  43.      private function setA($age)
  44.      {
  45.          $this->age=$age;
  46.  
  47.      }
  48.  
  49.      function __construct($name,$age)
  50.      {
  51.          $this->setN($name);
  52.          $this->setA($age);
  53.  
  54.      }
  55.  
  56.      function __toString()
  57.      {
  58.  
  59.          return "Hello  ". $this->getN() . "  Your Age is  " .   $this->getA();
  60.  
  61.  
  62.  
  63.      }
  64.  
  65.  
  66.  }
  67.  
  68. $ob= new Student('Sadia',22);
  69.  
  70.  echo $ob;
  71.  
  72. ?>
  73.  
  74. </body>
  75. </html>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top