Midterms - Object Oriented Programming in PHP Flashcards

1
Q

Defining a class

A

class keyword, class name, curly brace

<?php
class Fruit {

//Properties
public $name;
public $color;

//Methods
function set_name($name){
$this->name = $name;
}

function get_name(){
return $this->name;
}
}
?>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Objects of a class are created using the

A

new keyword

$apple = new Fruit ();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

refers to the current object, and is only available inside methods.

A

$this keyword

 <?php
class Fruit {
  public $name;
  function set_name($name) {
    $this->name = $name;
  }
}
$apple = new Fruit();
$apple->set_name("Apple");

echo $apple->name;
?> 
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

using $this keyword to change the value of a property

A
<?php

class Fruit{
public $name;

function set_name ($name){
$this->name = $name;

}
}

$apple = new Fruit();
$apple->set_name("Apple");

echo $apple->name;
?>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

ou can use the ____ keyword to check if an object belongs to a specific class:

A

instanceof

<?php
$apple = new Fruit();
var_dump($apple instanceof Fruit);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

allows you to initialize an object’s properties upon creation of the object

A

constructor

function_construct($name){
$this->name = $name;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

is called when the object is destructed or the script is stopped or exited

A

destruct Function

function_destruct(){
echo "The fruit is ($this->name).";
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

the property or method can be accessed from everywhere. This is default

A

public

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

the property or method can be accessed within the class and by classes derived from that class

A

protected

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

the property or method can ONLY be accessed within the class

A

private

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

When a class derives from another class.
The child class will inherit all the public and protected properties and methods from the parent class. In addition, it can have its own properties and methods

A

Inheritance

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

An inherited class is defined by using the

A

extends keyword
~~~
class Strawberry extends Fruit {
}
~~~

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

Inherited methods can be overridden by redefining the methods (use the same name) in the child class

A

Overriding

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

can be used to prevent class inheritance or to prevent method overriding.

A

final keyword
~~~
final class Fruit{
}
~~~

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

cannot be changed once it is declared

A

costant

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

Class constants are
A. case sensitive
B. case insensitive

A

A. case sensitive

17
Q

how to access constant from outside the class?

A

class name, scope resolution operator (::), constant name
~~~
<?php
class Goodbye{
const LEAVING_MESSAGE = “message”;
}

echo Goodbye::LEAVING_MESSAGE;
?>
~~~

18
Q

how to access constant from inside the class?

A

self keyword, scope resolution operator (::), constant name

<?php
class Goodbye{
const LEAVING_MESSAGE = "message";
public function byebye(){
echo self::LEAVING_MESSAGE;
}
}
$goodbye = new Goodbye();
$goodbye->byebye();
?>
19
Q

are when the parent class has a named method, but need its child class(es) to fill out the tasks

A

Abstract Classes

20
Q

is a method that is declared, but not implemented in the code.

A

abstract method

21
Q

allow you to specify what methods a class should implement.

A

Interfaces

22
Q

Interfaces are declared with the

A

interface keyword

<?php
interface InterfaceName{
}
?>
23
Q

____ cannot have properties but ____ can

A

Interfaces, Abstract classes

24
Q

interface methods must be

A

public

25
Q

abstract class methods must be

A

public or protected

26
Q

When one or more classes use the same interface, it is referred to as

A

polymorphism

27
Q

to implement an interface, a class must use the

A

implements keyword
~~~
<?php
interface Animal{
public function makeSound()
}

class Cat implements Animal{
public function makesound(){
echo “Meow”;
}
}

$animal = new Cat ();
$animal->makesound();
?>
~~~

28
Q

A user-defined function declaration starts with the

A

function keyword
~~~
function myMessage(){
echo “Hello World”;
}
~~~

29
Q

Information can be passed to functions through

A

arguments

30
Q

Arguments are specified after the function name, inside the

A

parentheses

function familyName($fname){
echo "$fname LastName.<br>";
}
familyName ("John");
31
Q

to let a function return a value use the

A

return statement

function sum ($x, y$){
$z = $x + $y;
return $z;
}
echo "5+10 = " . sum(5,10) . "<br>";
32
Q

To turn a function argument into a reference, use the

A

& operator

function add_five(&$value){
$value += 5;
}

$num = 2;
add_five ($num);
echo $num;
33
Q

array function for counting arrays

A

count ()

$cars = array ("Volvo", "BMW", "Toyota");
echo count ($cars);
34
Q

To access an array item you can refer to the

A

index number

$cars = array ("Volvo", "BMW", "Toyota");
echo $cars[0];
35
Q

are arrays that use named keys that you assign to them

A

Associative arrays

$car = array (“brand” => “Ford”, “model” => “Mustang”, “year” => 1964);

echo $car [“model”];