0

Here is the class file:

<?php
class user{

function _construct($firstName, $lastName, $username, $email, $password){
    $this->firstName    = $firstName;
    $this->lastName     = $lastName;
    $this->username     = $username;
    $this->email        = $email;
    $this->password     = $password;
}

public function getFirstName(){
    return $this->firstName;
}

public function getLastName(){
    return $this->lastName;
}

public function getUsername(){
    return $this->username;
}

public function getEmail(){
    return $this->email;
}

public function getPassword(){
    return $this->password;
}
}

Here is the script calling the class file:

<?php
require $_SERVER['DOCUMENT_ROOT'] . '/classes/user.php';
$user1 = new     user('Kyle','Birch','birchk1','[email protected]','195822Kb');
echo $user1->getFirstName() . ' is the first name';

Here is the error and display as a result:

Notice: Undefined property: user::$firstName in /Applications/XAMPP/xamppfiles/htdocs/classes/user.php on line 13
is the first name

Why isn't this calling the get method properly? I want to make sure I use proper coding practices, so even though I could just use public methods/variables without constructs, I prefer to do it properly.

5
  • 2
    _construct needs 2x so do: _ construct Commented Jul 24, 2015 at 22:14
  • As you are not specifically defining the class properties they will get defined as public, so you dont actually need all the getxxx() methods. They can be accessed simply as $user1->firstName Commented Jul 24, 2015 at 22:16
  • Thank you! That is the stupidest bit of syntax I've seen this side of a goto. Commented Jul 24, 2015 at 22:17
  • @RiggsFolly, how do I define the properties as protected? do I do it within the construct? Commented Jul 24, 2015 at 22:17
  • 1
    @KyleBirch 1) Use an IDE and the constructor will be highlighted with some nice shiny colors, if not then you probably have a typo :) 2) See: php.net/manual/en/language.oop5.properties.php how to initialize your properties Commented Jul 24, 2015 at 22:19

1 Answer 1

1

EDIT

I just realized that your _construct function only has one "_", it needs two underscores: "__" to be syntactically correct

For good OOP practices you need to add the variables as class variables:

<?php
class user{

protected $firstName;
protected $lastName;
protected $username;
protected $email;
protected $password;

function __construct($firstName, $lastName, $username, $email, $password){
    $this->firstName    = $firstName;
    $this->lastName     = $lastName;
    $this->username     = $username;
    $this->email        = $email;
    $this->password     = $password;
}

public function getFirstName(){
    return $this->firstName;
}

public function getLastName(){
    return $this->lastName;
}

public function getUsername(){
    return $this->username;
}

public function getEmail(){
    return $this->email;
}

public function getPassword(){
    return $this->password;
}
}
Sign up to request clarification or add additional context in comments.

4 Comments

I mention good OOP practices, because since I don't see any setters, it seems that @KyleBirch doesn't want them to be muttable, which is why they are protected and not public.
__construct() is how you define a constructor in PHP
Read my comment above ^^ again look at your code, read it again, then you know what I mean.
ahh gotcha! I copy pasted the code form Kyle, which led me to realize his other mistake of only putting one underscore. Good catch ^_^

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.