What's new

Help Mga mamaw sa Programming Kailangan ko tulong nyo (PHP)

M H I N Y E

Forum Guru
Elite
Joined
May 11, 2018
Posts
3,629
Solutions
5
Reaction
5,767
Points
1,610
KAILANGAN KO TULONG NYO MGA PAPS


We will create an Admin class, which is a child class of the User class. Follow and write the equivalent PHP scripts or answer with an explanation from the instructions listed below.


1. Create a user class.

2. Add to the class a private property with the name of $username.

3. Create a setter method that can set the value of the $username.

4. Create a class Admin that inherits the User class.

5. Now, let’s add to the Admin class some code. First, add a public method by the name of expressYourRole, and make it return the string: “Admin”.

6. Add to the Admin class another public method, sayHello, that returns the string “Hello admin, XXX” with the username instead of XXX.

7. Create an object $admin1 out of the class Admin, set its name to “Balthazar”, and sayhello to the user. Do you see any problem?

8. What do you think is the cause of the problem?

9. How will you fix this problem?
 
May nasimulan ka na ba? Kung meron pwede paki share? hahaha kung tinuro na sa nyo mga class, ung pinagpagawa sa inyo pinagsama samang mga example lng ng module :D
 
PHP:
<?php
class User {
  private $username; // <--- change to public
  public function __construct($username) {
    $this->username = $username;
  }
}

class Admin extends User {
  public function expressYourRole() {
    echo "admin";
  }
 
  public function sayHello() {
    echo "Hello admin, {$this->username}";
  }
}

$Balthazar= new Admin("Balthazar");
$Balthazar->sayHello();
?>

just change the private $username to public para lumabas sya sa sayHello() na function
 

Similar threads

Back
Top