PHP Set 8 Flashcards

1
Q

What does a child class inherit from its parent class?

A

All public and protected properties and methods

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

How is an inherited class defined?

A

Using the extends keyword
Ex: class Apple extends Food { }

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

How can an inherited method be overridden in a child class?

A

By redefining a method using the same name in the child class

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

How can method overriding or class inheritance be prevented?

A

Using the final keyword prevents child classes from being made from an object or a method being redefined

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

What is an abstract class?

A

A class that has at least one abstract method

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

What is an abstract method?

A

A method that is declared with the abstract keywork and not implemented in code

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

What is the syntax for declaring an abstract function?

A

abstract function name($param): returntype;

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

What are the rules for when a child class is inherited from an abstract class?

A
  1. The child class method must be defined with the same name and redeclare the parent abstract method
  2. The child class method must be defined with the same or a less restricted access modifier
  3. The number of required arguments must be the same but the child class can add optional arguments
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the difference between an interface and an abstract class?

A
  1. Interfaces cannot have properties but abstract classes can
  2. All interface methods are abstract so they abstract keyword isn’t necessary
  3. All interface methods must be public
  4. Classes can implement an interface and inherit from another class at the same time
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you declare an interface and how do you implement one?

A

Declare:
interface Animal {
functions
}
Implement:
class Cat implements Animal { }

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

What are the 2 methods used to connect to MySQL?

A
  1. MySQLi extension
  2. PDO (PHP Data Objects)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the main difference between MySQLi and PDO?

A

PDO supports various databases but MySQLi only supports MySQL
MySQLi is a but faster
PDO supports 12 different drivers but MySQLi only supports MySQL

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

How do you open a MySQL connection using MySQLi?

A

$conn = new mysqli(“localhost”, “username”, “password”, “dbName”)

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

How do you open a MySQL connection using PDO?

A

$conn = new PDO($dsn, “root”, “lsucceed168”, $options)
where $dsn = “mysql:host=localhost;dbname=name;charset=utf8mb4”

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

How do you create a table in MySQL?

A

With the CREATE TABLE statement
$sql = “CREATE TABLE MyGuests (id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL, lastname VARCHAR(30) NOT NULL, email VARCHAR(50),
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP )”;

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

How do you perform an insert to a table in MySQL?

A

With the INSERT INTO statement
$sql = “INSERT INTO MyGuests (firstname, lastname, email) VALUES (‘John’,
‘Doe’, ‘john@example.com’)”;

17
Q

What is the difference between MySQLi and PDO when detecting command errors?

A

With MySQLi you can check the return value in an if statement
With PDO you have to use a try-catch block to detect errors

18
Q

What is a MySQL prepared statement?

A

A technique used to execute the same or similar SQL statements repeatedly with high efficiency

19
Q

What are the procedures for creating a prepared MySQL statement?

A
  1. Prepare: create an SQL query with empty values as placeholders with a question mark or variable name
  2. Bind: Bind values or variables to the placeholders
  3. Execute the query simultaneously
20
Q

What is the syntax to execute a prepared statement with MySQLi?

A

$sql = $conn->prepare(“INSERT INTO
myguests(firstname, lastnamen,
email) VALUES (?, ?, ?)”);
$sql->bind_param(“sss”, $fn, $ln,
$email); // “sss” means that $fn, $ln
and $email as a string
$fn = “Mary”;
$ln = “Gao”;
$email = “mary_gao@gmail.com”;
$sql->execute();

21
Q

What are the benefits of using prepared SQL statements?

A
  1. They are useful against SQL injection attacks
  2. They reduce parsing time as the preparation of the query is only done once when the same statement is needed repeatedly
  3. Minimize server bandwidth as you need to send only the parameters each time and not the whole statement
22
Q

What data types can be passed in when using bind_param?

A

i for integer
d for double
s for string
b for binary large object

23
Q

What is the syntax to execute a prepared statement with PDO?

A

$stmt = $conn->prepare(“INSERT INTO
MyGuests (firstname, lastname,
email)
VALUES (:firstname, :lastname,
:email)”);
$stmt->bindParam(‘:firstname’,
$firstname);
$stmt->bindParam(‘:lastname’,
$lastname);
$stmt->bindParam(‘:email’, $email);
$firstname = “Mary”;
$lastname = “Gao”;
$email = “mary.gao@example.com”;
$stmt->execute();

24
Q

What are the other differences between MySQLi and PDO?

A
  1. PDO allows for named parameters but with MySQLi you can only use ?
  2. PDO does not allow using named and positional parameters in the same query