Guest User

Untitled

a guest
Sep 8th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  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. function speak() {
  59. echo "meow";
  60. }
  61. }
  62.  
  63. include('class_cat.php');
  64. mycat = new cat;
  65. $speak = cat->speak();
  66. echo $speak;
  67.  
  68. <?php
  69. // The code below creates the class
  70. class Person {
  71. // Creating some properties (variables tied to an object)
  72. public $isAlive = true;
  73. public $firstname;
  74. public $lastname;
  75. public $age;
  76.  
  77. // Assigning the values
  78. public function __construct($firstname, $lastname, $age) {
  79. $this->firstname = $firstname;
  80. $this->lastname = $lastname;
  81. $this->age = $age;
  82. }
  83.  
  84. // Creating a method (function tied to an object)
  85. public function greet() {
  86. return "Hello, my name is " . $this->firstname . " " . $this->lastname . ". Nice to meet you! :-)";
  87. }
  88. }
  89.  
  90. // Creating a new person called "boring 12345", who is 12345 years old ;-)
  91. $me = new Person('boring', '12345', 12345);
  92.  
  93. // Printing out, what the greet method returns
  94. echo $me->greet();
  95. ?>
  96.  
  97. // Create a new class, and include a __construct method
  98. class Task {
  99.  
  100. public $title;
  101. public $description;
  102.  
  103. public function __construct($title, $description){
  104. $this->title = $title;
  105. $this->description = $description;
  106. }
  107. }
  108.  
  109. // Create a new object, passing in a $title and $description
  110. $task = new Task('Learn OOP','This is a description');
  111.  
  112. // Try it and see
  113. var_dump($task->title, $task->description);
  114.  
  115. class Person{
  116. private $fname;
  117. private $lname;
  118.  
  119. public function __construct($fname,$lname){
  120. $this->fname = $fname;
  121. $this->lname = $lname;
  122. }
  123. }
  124. $objPerson1 = new Person('john','smith');
  125.  
  126. class Test
  127. {
  128. function __construct($value1,$value2)
  129. {
  130. echo "Inside Construct";
  131. echo $this->value1;
  132. echo $this->value2;
  133. }
  134. }
  135.  
  136. //
  137. $testObject = new Test('abc','123');
Add Comment
Please, Sign In to add comment