Properties and Methods Flashcards

1
Q

The scopes of a property

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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?

A

protected

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What three keywords define the access scope of a property?

A

protected, public, & private

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the following symbols called “->”?

A

Object Operator

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

To access a property of an object you use the object operator which is made up which characters?

A

->

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What best describes the $this pseudo variable?

A

An internal reference to the current instance of an object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which of the following best describes a class property?

A

A variable that is defined inside of a class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  1. Add the following public properties to the Fish class: common_name, flavor, and record_weight.

<?php </p>

class Fish {

}

?>

  1. Define a constructor on the Fish class with 3 parameters, in order, named $name, $flavor, and $record.
  2. In the new constructor method, assign each of the properties on the Fish class with its corresponding parameter variable.
  3. 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.”

A

<?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();

?>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly