Chapter 5 - PHP Functions and Objects Flashcards

1
Q

What are five benefits of using functions instead of contiguous, inline code?

A
  • Involve less typing
  • Reduce syntax and other programming errors
  • Decrease the loading time of program files
  • Decrease execution time, because each function is compiled only once, no matter how often you call it
  • Accept arguments and can therefore be used for general as well as specific cases
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does the ‘include’ statement do?

A

Using include, we can tell PHP to fetch a particular file and load all its contents. It’s as if you pasted the included file into the current file at the insertion point.

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

What does the include_once statement do?

A

Each time you issue the include directive, it includes the requested file again, even if you’ve already inserted it. For instance, suppose that library.php contains a lot of useful functions, so you include it in your file, but also include another library that includes library.php. Through nesting, you’ve included library.php twice, by mistake.

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

What does the include_once statement do?

A

Each time you issue the include directive, it includes the requested file again, even if you’ve already inserted it. For instance, suppose that library.php contains a lot of useful functions, so you include it in your file, but also include another library that includes library.php. Through nesting, you’ve included library.php twice, by mistake.

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

What are require and require_once used for?

A

A potential problem with include and include_once is that PHP will only attempt to include the requested file. Program execution continues even if the file is not found.

When the file is essential to the program, it should be ‘required’. If the document is not fund, it will not run and will abort.

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

What are require and require_once used for?

A

A potential problem with include and include_once is that PHP will only attempt to include the requested file. Program execution continues even if the file is not found.

When the file is essential to the program, it should be ‘required’. If the document is not fund, it will not run and will abort.

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

How do you ensure PHP version compatibility?

A

PHP is an ongoing process of development, and there are multiple versions. If you need to check whether a particular function is available to your code, you can use the function_exists function, which checks all predefined and user-created functions.

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

In terms of objects, what is a class?

A

When creating a program to use objects, you need to design a composite of data and code called a class.

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

In terms of an object, what is an instance or occurrence of a class?

A

Each new object based on a class is called an instance.

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

In terms of an object, what is an instance or occurrence of a class?

A

Each new object based on a class is called an instance.

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

What are properties?

A

The data associated with an object.

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

What are the functions used by an object?

A

Methods

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

Give a example of declaring a class:

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

How do you create an object?

A

$object = new User; $temp = new User(‘name’, ‘password’);

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

What is a constructor?

A

When creating a new object, you can pass a list of arguments to the class being called. These are passed to a special method within the class, called the constructor, which initializes various properties.

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

What is a constructor?

A

When creating a new object, you can pass a list of arguments to the class being called. These are passed to a special method within the class, called the constructor, which initializes various properties.

17
Q

What is the main benefit of using a function?

A

Using functions avoids the need to copy or rewrite similar code sections many times over by combining sets of statements together so that they can be called by a simple name.

18
Q

How many values can a function return?

A

By default, a function can return a single value. But by utilizing arrays, references and global variables, any number of values can be returned.

19
Q

What is the difference between accessing a variable by name and by reference?

A

When you reference a variable by name, such as by assigning its value to another variable or by passing its value to a function, its value is copied. The original does not change when the copy is changed. But if you reference a variable, only a pointer (or reference) to its value is used, so that a single value is referenced by more than one name. Changing the value of the reference will change the original as well.

20
Q

What is the meaning of scope in PHP?

A

Scope refers to which parts of a program can access a variable. For example, a variable of global scope can be accessed by all parts of a PHP program.

21
Q

How can you incorporate one PHP file within another?

A

To incorporate one file within another, you can use the include or require directives, or their safer variants, include_once and require_once.

22
Q

How is an object different from a function?

A

A function is a set of statements referenced by a name that can receive and return values. An object may contain zero or many functions (which are then called methods) as well as variables (which are called properties), all combined in a single unit.

23
Q

HOw o you create a new object in PHP?

A

To create a new object in PHP, use the new keyword like this: $object = new Class;

24
Q

What syntax would you use to create a subclass from an existing one?

A

To create a subclass, use the extends keyword with syntax such as this: class Subclass extends Parentclass …

25
Q

HOw can you call an initializing piece of code when an object is created?

A

To call a piece of initializing code when an object is created, create a constructor method called __construct within the class and place your code there.

26
Q

Why is it a good idea to explicitly declare properties within a class?

A

Explicitly declaring properties within a class is unnecessary, as they will be implicitly declared upon first use. But it is considered good practice as it helps with code readability and debugging, and is especially useful to other people who may have to maintain your code.