Chapter 5 - PHP Functions and Objects Flashcards
What are five benefits of using functions instead of contiguous, inline code?
- 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
What does the ‘include’ statement do?
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.
What does the include_once statement do?
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.
What does the include_once statement do?
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.
What are require and require_once used for?
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.
What are require and require_once used for?
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 do you ensure PHP version compatibility?
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.
In terms of objects, what is a class?
When creating a program to use objects, you need to design a composite of data and code called a class.
In terms of an object, what is an instance or occurrence of a class?
Each new object based on a class is called an instance.
In terms of an object, what is an instance or occurrence of a class?
Each new object based on a class is called an instance.
What are properties?
The data associated with an object.
What are the functions used by an object?
Methods
Give a example of declaring a class:
How do you create an object?
$object = new User; $temp = new User(‘name’, ‘password’);
What is a constructor?
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.
What is a constructor?
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.
What is the main benefit of using a function?
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.
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 a 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 within another?
To incorporate one file within another, you can use the include or require directives, or their safer variants, 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 many functions (which are then called methods) as well as variables (which are called properties), all combined in a single unit.
HOw o you create a new object in PHP?
To create a new object in PHP, use the new keyword like this: $object = new Class;
What syntax would you use to create a subclass from an existing one?
To create a subclass, use the extends keyword with syntax such as this: 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.