This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

what is the function __construct used for

By: a guest on Feb 26th, 2012  |  syntax: None  |  size: 1.04 KB  |  views: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. class Database {
  2.   protected $userName;
  3.   protected $password;
  4.   protected $dbName;
  5.  
  6.   public function __construct ( $UserName, $Password, $DbName ) {
  7.     $this->userName = $UserName;
  8.     $this->password = $Password;
  9.     $this->dbName = $DbName;
  10.   }
  11. }
  12.  
  13. // and you would use this as:
  14. $db = new Database ( 'user_name', 'password', 'database_name' );
  15.        
  16. class Person {
  17.  
  18.     public function __construct() {
  19.         // Code called for each new Person we create
  20.     }
  21.  
  22. }
  23.  
  24. $person = new Person();
  25.        
  26. class Person {
  27.  
  28.     public $name = '';
  29.  
  30.     public function __construct( $name ) {
  31.         $this->name = $name;
  32.     }
  33.  
  34. }
  35.  
  36. $person = new Person( "Joe" );
  37. echo $person->name;
  38.        
  39. class Cat
  40. {
  41.     function Cat()
  42.     {
  43.         echo 'meow';
  44.     }
  45. }
  46.        
  47. class Cat
  48. {
  49.     function __construct()
  50.     {
  51.         echo 'meow';
  52.     }
  53. }
  54.        
  55. $cat = new Cat();
  56.        
  57. class Cat
  58. {
  59.     function Cat()
  60.     {
  61.         echo 'meow';
  62.     }
  63. }
  64.        
  65. class cat {
  66.     function speak() {
  67.         echo "meow";  
  68.     }
  69. }
  70.        
  71. include('class_cat.php');
  72. mycat = new cat;
  73. $speak = cat->speak();
  74. echo $speak;
clone this paste RAW Paste Data