Jake Psimos
Jake Psimos

Reputation: 11

PHP variable scope within classes

I am trying to make a class that simply holds and retrieves simple string variables.

Here is what i have in the User.php file (the class)

class User {

//private variables
private $u_s;
private $p_w;

public function _construct($u_s, $p_w){
    $this->u_s = $u_s;
    $this->p_w = md5($p_w);
}

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

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

}

and here is the index.php file

    <?php
       include("User.php");

       $u = new User("Jake", "pass");
       echo $u->getUsername() + "\n" + $u->getPassword();
    ?>

Why does this not work?

Upvotes: 0

Views: 481

Answers (2)

Jerzy Zawadzki
Jerzy Zawadzki

Reputation: 1985

Concat char in PHP is . (dot), not + (plus) sign:

<?php
include("User.php");

$u = new User("Jake", "pass");
echo $u->getUsername() . "\n" . $u->getPassword();

And as adpalumbo said you mistyped __construct

Upvotes: 1

adpalumbo
adpalumbo

Reputation: 3031

You mistyped __construct. There should be two underscores, not one.

Upvotes: 2

Related Questions