2

I am a 2nd Year IT student just started with OOP in PHP and i'm really struggling to get the hang of it, I have been coding in a procedural way for years so please understand making the switch to OOP is extremely challenging, and I am yet to see the benefit of it, anyway just had to get that of my chest.

Question enter image description here enter image description here enter image description here enter image description here

According to the above question I came above with the above code,, which I tweaked to infinity, my prescribed book is also not very helpful

CODE

class Registration{
        private $user_type;
        private $user_name;

        function __construct($user_type, $user_name){
            $this->user_type=$user_type;
            $this->user_name=$user_name;
        }//constructor

        function setUser($user_type, $user_name){
            if($user_type == "admin"){
                $user_name = "Peter";
                $msg = "Hi administrator ".$user_name;  
            }
            else if($user_type="member"){
                $user_name = "Ntubele123!";
                $msg = "Hi member ".$user_name; 
            }
        }//function

        function getUser(){
            return $this->user_type;
        }//function getter

}//class

    $userInfo = new Registration($user_type, $user_name);
        $user = $userInfo->setUser("admin", "Peter");
        $user = $userInfo->getUser();

MY QUESTIONS

  • I suspect a lot is wrong with the above code, if someone could be kind enough to let me know where I am going wrong and what I should consider changing, keeping beginner friendly in mind, it would be greatly appreciated.

ERROR

Undefined variable user_type & user_name

4
  • 1
    before the line starting with $userInfo = new ... you need to create the $user_type and $user_name variables you're passing to new Registration($user_type, $user_name.. $user_type = $user_name = ''; should do Commented May 8, 2016 at 6:18
  • Your setter method isn't setting anything. It's ignoring the username that's passed in, and always showing greetings for Peter or Ntubele123. Commented May 8, 2016 at 6:42
  • @Barmar it is showing nothing mate only prints admin on screen when initializing the object as new Registration("admin", "Peter") aaaarrrrrgggghhhh Commented May 8, 2016 at 6:48
  • I'm talking about setUser, which is supposed to set the user to the name given in the argument, but just displays a message. Commented May 8, 2016 at 6:50

2 Answers 2

1
$userInfo = new Registration($user_type, $user_name);

In this line $user_type, $user_name is undefined because no variables declared with this name

So declare the variable at first than create object of Registration class

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you for your help and input....When I do that by initializing the object as new Registration("admin", "Peter") I only get the output admin printed on screen....? So clearly some of my methods are wrong? –
you have to define variable before $userInfo = new Registration($user_type, $user_name);
OK sure but where do I define that variable in one of my methods?
Tell me what you exactly need. when you want set user?
I want SetUser to be as follows When user_type = admin and user_name = peter I want message displayed Hi Admin Peter else I want messaged displayed Hi member Ntubele123
|
1

There's nothing different about using variables in OOP code. You still have to set them before you can use them as arguments. So it should be:

$name = "Fred";
$type = "admin";
$user = new Registration($type, $name);

Your setter method is supposed to set the value of the property. So it should be:

function setUser($username) {
    $this->user_name = $username;
}

A setter method should normally just set one property at a time.

The whole class should then look like:

class Registration{
    private $user_type;
    private $user_name;

    function __construct($user_type, $user_name){
        $this->user_type=$user_type;
        $this->user_name=$user_name;
    }//constructor

    function setUser($user_name){
        $this->user_name = $user_name;
    }

    function setType($type) {
        $this->user_type = $type;
    }

    function getUser(){
        return $this->user_name;
    }//function getter

    function getType() {
        return $this->user_type;
    }

    function greet() {
        echo "Hello " . $this->user_type . " " . $this->user_name;
    }

}//class

$user = new Registration("admin", "Joe");
$user->greet();

1 Comment

Thank you very much for your effort and insight

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.