Properties and Methods Flashcards
The scopes of a property
- public
- accessed from anywhere
- protected
- accessed only from the class itself as well as any parent or inherited classes
- private
- accessed only from the class that defined them
If I wanted to define a property or method that was available to the class that created it and its children, what access scope keyword should I use?
protected
What three keywords define the access scope of a property?
protected, public, & private
What are the following symbols called “->”?
Object Operator
To access a property of an object you use the object operator which is made up which characters?
->
What best describes the $this pseudo variable?
An internal reference to the current instance of an object.
Which of the following best describes a class property?
A variable that is defined inside of a class.
- Add the following public properties to the Fish class: common_name, flavor, and record_weight.
<?php </p>
class Fish {
}
?>
- Define a constructor on the Fish class with 3 parameters, in order, named $name, $flavor, and $record.
- In the new constructor method, assign each of the properties on the Fish class with its corresponding parameter variable.
- Create a Fish object named $bass with the following data:
name: “Largemouth Bass”
flavor: “Excellent”
record: “22 pounds 5 ounces”
1. Create a method on Fish named getInfo that takes no parameters and returns a string that includes the common_name, flavor, and record_weight for the fish. When called on $bass, getInfo might return “A Largemouth Bass is an Excellent flavored fish. The world record weight is 22 pounds 5 ounces.”
<?php </p>
class Fish {
public $common_name;
public $flavor;
public $record_weight;
function __construct($name, $flavor, $record) {
$this->common_name = $name;
$this->flavor = $flavor;
$this->record_weight = $record;
}
public function getInfo() {
return “A $this->common_name . is an $this->flavor . fish. The world record weight is $this->record_weight”;
}
}
$bass = new Fish("Largemouth Bass", "Excellent", "22 pounds 5 ounces"); echo $bass-\>getinfo();
?>