1

I am creating a class with variable, as you can see from the code below, I am want to access

$allen = new guest(...); echo $allen->userName;

instead I have to use this

$allen->getUserName();

but since username won't change after the class is created, is there a way to do this?

<?php 
    class guest {

        var $firstName;
        var $lastName; 
        var $email;
        var $userName;

        function guest($firstName, $lastName, $email) {
            $this->firstName = $firstName; 
            $this->lastName = $lastName; 
            $this->userName = getUserName();  // option 1 
            $this->userName = $this->getUserName(); // option 2

            preg_match('/^.*(?=(@))/i', $this->email, $userName);
            $this->userName = $userName[0];  // this is messy but work


        }
        function getUserName() {
            preg_match('/^.*(?=(@))/i', $this->email, $userName);
            return $userName[0];
        }
    }
?>   
4
  • Using var for properties in a PHP class is a throwback to PHP 4, only maintained for backward compatibility..... but it is recommended that you use public/protected/private visibility instead.... however, as var is treated as public, echo $allen->username; is perfectly valid Commented Jun 13, 2015 at 19:18
  • If you don't want $username to be changed after the class is instantiated, set proper visibilities for your properties Commented Jun 13, 2015 at 19:20
  • Are you sure this is your real code, and it works? getUserName() refers to $userName, not $this->userName? Commented Jun 13, 2015 at 20:15
  • no, opt 1 and opt 2, it doesn't work, it seem there are conflict b/c getUserName function is written after the __construct. ? Commented Jun 13, 2015 at 20:17

2 Answers 2

2

In php5 constructors are not named after the class any more. Instead define it like that:

public function __construct($firstName, $lastName, $email) 
{
    ///...
}

Next, it is unclear how you actually set the property $this->userName... you write "this is messy, but it works", however I don't see how this is meant to work, since $userName should be an array after calling the preg_match(). And saving the array does not make any sense...

Further: if you really define the property $this->userName inside the constructor (which makes sense), then why does the getter method getUserName() reparse other properties? Instead a plain getter should only return the properties value without much interpretation. That should be fine, since you want to set the value inside the constructor (which probably currently does not work). Maybe this is just a leftover from previous attempts. I just wanted to mention it since it looks odd...

Have fun!

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

1 Comment

thanks, yeah i was trying different way to do it, and confuse myself, over it. I edit the code, to fix the array part, I guess it will be ok to interpreted inside constructor, instead of running a code each time, the value is need.
1

In order to deny direct access to your class attribute, you can define it as private. By doing that, construction like $allen->userName will throw an error. This is also known as encapsulation.

First make sure that you initialize your class object properly. In PHP you should define your constructor using __constructor function. Here is example:

       class Guest {
    protected $lastName;
    protected $email;
    protected $lastName;
    protected $userName;

    public function __construct($firstName, $lastName, $email, $userName){
        $this->firstName = $firstName;
        $this->lastName = $lastName;
        $this->email = $email;
        $this->userName = $userName;
    }
    public function getUsername(){
        return $this->userName;
    }      
}

The constructor receives 4 parameters $firstName, $lastName, $email, $userName. In order to create an object of this class you can execute the following code:

$allen = new Guest('John', 'Doe', '[email protected]', 'username');

To retrieve an username, write the following line:

$allen->getUsername();

In your current constructor something is bad, becuase I think your userName attribute is not properly initialized.

Comments

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.