ch5 questions Flashcards
What is the main benefit of using a function?
Less typing, reduced syntax and other programming errors, decreased loading time of program files. Decreased execution time, because each function is compiled only once no matter how often you call it.
Accepts arguments and can therefor be used for general as well as specific cases
How many values can a function return?
By default, a function can return a single value. But by utilizing arrays, references, and global variables, any number of values can be returned.
What is the difference between accessing a variable by name and by reference?
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 single value is referenced by more than one name. Changing the value of the reference will change the original as well.
What is the meaning of scope in PHP?
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.
How can you incorporate one PHP file with another?
Using include or require directives, or their safer varients, include_once and require_once.
How is an object different from a function?
A function is a set of statements referenced by a name that can receive and return values. An object may contain zero or as many functions (which are then called methods) as well as variables (which are called properties), all combined in a single unit.
How do you create a new object in PHP?
$object = new Class;
What syntax would you use to create a subclass from an existing one?
using the extends keyword
ex:
class Subclass extends Parentclass
How can you call an initializing piece of code when an object is created?
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.
Why is it a good idea to explicitly declare properties within a class?
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.