patternphpModerate
Human class implementation
Viewed 0 times
implementationhumanclass
Problem
I just want an indication as to whether or not I'm on the right track regarding PHP OOP, at least on a basic level. Positive criticism welcome.
P.S. Excuse the visuals of the code. This is how I usually remember code.
```
gender = $gender;
$this->name = $name;
$this->age = $age;
$this->talk = $talk;
}
public function getGender(){
return $this->gender;
}
public function getName(){
return $this->name;
}
public function getAge(){
return $this->age;
}
public function getTalk(){
return $this->talk;
}
}
class female extends Human{
//properties of female
private $bum;
private $feet;
public function __construct($gender, $name, $age, $talk, $bum, $feet){
parent::__construct($gender, $name, $age, $talk);
$this->bum = $bum;
$this->feet = $feet;
}
public function getBumSize(){
return $this->bum;
}
public function getFeetSize(){
return $this->feet;
}
}
class male extends Human{
//properties of male
private $facialHair;
private $adamsApple;
public function __construct($gender, $name, $age, $talk, $facialHair, $adamsApple){
parent::__construct($gender, $name, $age, $talk);
$this->beard = $facialHair;
$this->throat = $adamsApple;
}
public function getBeard(){
return $this->beard;
}
public function getThroat(){
return $this->throat;
}
}
//instantiate objects
$malePerson = new male('male', 'John Doe', '32', 'clear', 'beard', 'adams apple in my throat');
$femalePerson = new female('female', 'Jane Doe', '28', 'loud', 'big and round', 'small');
echo "Hi, My name is {$malePerson->getName()}, {$malePerson->getAge()} years of age, I talk {$malePerson->getTalk()}
so i guess that makes me {$malePerson->getGender()} a.k.a man....I also have
a {$malePerson->getThroat()} and a {$ma
P.S. Excuse the visuals of the code. This is how I usually remember code.
```
gender = $gender;
$this->name = $name;
$this->age = $age;
$this->talk = $talk;
}
public function getGender(){
return $this->gender;
}
public function getName(){
return $this->name;
}
public function getAge(){
return $this->age;
}
public function getTalk(){
return $this->talk;
}
}
class female extends Human{
//properties of female
private $bum;
private $feet;
public function __construct($gender, $name, $age, $talk, $bum, $feet){
parent::__construct($gender, $name, $age, $talk);
$this->bum = $bum;
$this->feet = $feet;
}
public function getBumSize(){
return $this->bum;
}
public function getFeetSize(){
return $this->feet;
}
}
class male extends Human{
//properties of male
private $facialHair;
private $adamsApple;
public function __construct($gender, $name, $age, $talk, $facialHair, $adamsApple){
parent::__construct($gender, $name, $age, $talk);
$this->beard = $facialHair;
$this->throat = $adamsApple;
}
public function getBeard(){
return $this->beard;
}
public function getThroat(){
return $this->throat;
}
}
//instantiate objects
$malePerson = new male('male', 'John Doe', '32', 'clear', 'beard', 'adams apple in my throat');
$femalePerson = new female('female', 'Jane Doe', '28', 'loud', 'big and round', 'small');
echo "Hi, My name is {$malePerson->getName()}, {$malePerson->getAge()} years of age, I talk {$malePerson->getTalk()}
so i guess that makes me {$malePerson->getGender()} a.k.a man....I also have
a {$malePerson->getThroat()} and a {$ma
Solution
First of all: Extract your classes into extra files and
Second: Learn and start to use PHPDoc to document your code.
That's not a helpful comment...only write a comment if it is helpful.
Such structural comments are unnecessary if your class is correctly structured and indented.
Personally I don't like this kind of intending. It can make code easier to read, but in a pure "assign all parameters to all fields" constructors it seems kinda unnecessary.
Is it a typo that
If it holds the size of the bum, then it should be called
Apart from the unnecessary intending, this is a good example stringly typed programming. You're using a string for everything even though the age should be an integer.
Let's have a look at the basic variables a
And let's go forth with the
Also forget what I just said about
That's not a well-formed HTML document.
Ideally you would perform initializations before the
require_once them as necessary. I found the Java style of having one class per file a very good one.Second: Learn and start to use PHPDoc to document your code.
//BlueprintThat's not a helpful comment...only write a comment if it is helpful.
// properties of human
private $gender;
private $name;
private $age;
private $talk;Such structural comments are unnecessary if your class is correctly structured and indented.
$this->gender = $gender;
$this->name = $name;
$this->age = $age;
$this->talk = $talk;Personally I don't like this kind of intending. It can make code easier to read, but in a pure "assign all parameters to all fields" constructors it seems kinda unnecessary.
class female extends Human{Is it a typo that
female is lower case?private $bum;If it holds the size of the bum, then it should be called
$bumSize. The same goes for $feet.$malePerson = new male('male', 'John Doe', '32', 'clear', 'beard', 'adams apple in my throat');Apart from the unnecessary intending, this is a good example stringly typed programming. You're using a string for everything even though the age should be an integer.
Let's have a look at the basic variables a
Human consists of:$genderShould an enumeration with two values,MaleandFemale.
$nameNames are strings, so it's okay.
$ageIs an integer and should therefor be an integer.
$talkIs also a string.
And let's go forth with the
Male:$facialHairShould be an enum, or an integer to indicate strength.
$adamsAppleNot sure what this is.
Also forget what I just said about
$gender, Human should be an abstract class and getGender() should be an abstract method that is implemented by the next class.
That's not a well-formed HTML document.
Title of your page
Content goes here.
Ideally you would perform initializations before the
html tag and then just use everything prepared in the body.Code Snippets
//Blueprint// properties of human
private $gender;
private $name;
private $age;
private $talk;$this->gender = $gender;
$this->name = $name;
$this->age = $age;
$this->talk = $talk;class female extends Human{private $bum;Context
StackExchange Code Review Q#41669, answer score: 13
Revisions (0)
No revisions yet.