php classes- need help

Hi there.
The thing is I am really bad at classes in php and I have an assignment:
“Create user class which contains user data, like: id, e-mail, password, name…
The class should also have a method to show all the user data.”

Is someone could help me or point me in the right direction please.
Thank you in advance.

What have you learnt about classes so far? What makes up a class?

A class builds an object.

[php]class Car {
public $color;
public $make;
public $model;
}[/php]

This is a basic class for a Car class. It doesn’t do anything, but describes a car.
Show what you have tried to this point.

This is what I did 'till now but it feels wrong:
[php]<?php
class info
{
public $id;
public $name;
public $email;
public $password;

public function setid($newval)
{
$this->id = $newval;
return $this->id . “
”;
}
public function setname($newval)
{
$this->name = $newval;
return $this->name . “
”;
}
public function setmail($newval)
{
$this->email = $newval;
return $this->email . “
”;
}
public function setpassword($newval)
{
$this->password = $newval;
return $this->password . “
”;
}

}
$id = new info;
echo $id->setid(1);
$name = new info;
echo $name->setname(“random”);
$email = new info;
echo $email->setmail("[email protected]");
$pass = new info;
echo $pass->setpassword(“123456789”);
?>[/php]

You’re creating a new instance of the class (a new object) for each property you set, you probably don’t want that.

[php]$info = new info;
$info->setid(1);
$info->setname(“random”);
$info->setmail("[email protected]");
$info->setpassword(“123456789”);
echo ‘

’;
print_r($info);[/php]

[hr]

Some comments:

The class name doesn’t reflect what it “is”. Yes it’s info, but that’s pretty much everything we’re doing when programming. “User”, “Person” or similar would make more sense.

The getters/setters are kinda redundant when the class properties are public (you can just do $instance->foo = ‘bar’). Though I’d advice to use the methods and change the properties to protected or private

A nice “trick” is to return $this on the setters. that way you can do the following - which looks a lot better and is usually preferred.

[php]$user = new User;
$user->setId(1)
->setName(“random”)
->setEmail("[email protected]")
->setPassword(“123456789”);
echo ‘

’;
print_r($user);[/php]

note, I also changed the setter method names, I have OCD on that stuff

[hr]

I strongly suggest you read up on this site:

I’ll start by mimicing Jim’s comments.

I was giving a simple example of a class. Generally, a class’s properties should be only as visible as they need to be, hence private, protected, public. Classes should be a blackbox. You send info to them, don’t know how they work and don’t care, just that you get out what you expect.

What book are you following? Read through it again and see what you glean.
[php]class Vehicle
{
private $_make;
private $_model;
private $_year;

public function __construct( $make, $model, $year ) {
    $this->_make = $make;
    $this->_model = $model;
    $this->_year = (int) $year;

}

public function __toString(){
return “This instance of Car is for a {$this->_year} {$this->_make} {$this->_model}.”;
}

}

$sport = new Vehicle( ‘Chevrolet’, ‘Corvette’, 2015);
$suv = new Vehicle(‘Jeep’, ‘Wrangler’, 1974);

echo “

$sport

”;
echo “

$suv

”;
[/php]

Classes give blueprints for what the class is.

Sponsor our Newsletter | Privacy Policy | Terms of Service