1 / 20

PHP Functions and Objects

PHP Functions and Objects. Chapter 5. The basic requirements of any programming language include somewhere to store data , a means of directing program flow, and a few bits and pieces such as expression evaluation , file management, and text output

julie
Download Presentation

PHP Functions and Objects

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP Functions and Objects Chapter 5

  2. The basic requirements of any programming language include somewhere to store data, a means of directing program flow, and a few bits and pieces such as expression evaluation, file management, and text output • A function is a set of statements that performs a particular function and—optionally—returns a value PHP Functions

  3. Less typing is involved. • Functions reduce syntax and other programming errors. • They decrease the loading time of program files. • They also decrease execution time, because each function is compiled only once, no matter how often you call it. • Functions accept arguments and can therefore be used for general as well as specific cases. Why Function Junction?

  4. An object incorporates one or more functions, and the data they use, into a single structure called a class Objects

  5. Hundreds of ready-made, built-in functions print("print is a function"); The parentheses tell PHP that you’re referring to a function PHP Functions

  6. Functions can take any number of arguments, including zero • Example 5-1. Three string functions <?php echostrrev(" .dlrowolleH"); // Reversestring echo str_repeat("Hip ", 2); // Repeatstring echo strtoupper("hooray!"); // String touppercase ?> Builtin Functions

  7. The general syntax for a function is: function function_name([parameter [, ...]]) { // Statements } Defining a function

  8. Let’s take a look at a simple function to convert a person’s full name to lowercase and then capitalize the first letter of each name. $lowered = strtolower("aNY # of Letters and Punctuation you WANT"); echo $lowered; Output any # of letters and punctuation you want Returning a Value

  9. Example 5-3. Returning multiple values in an array <?php $names = fix_names("WILLIAM", "henry“, "gatES"); echo $names[0] . " " . $names[1] . " " . $names[2]; function fix_names($n1, $n2, $n3) { $n1 = ucfirst(strtolower($n1)); $n2 = ucfirst(strtolower($n2)); $n3 = ucfirst(strtolower($n3)); return array($n1, $n2, $n3); } ?> Returning an Array

  10. Example 5-5. Returning values in global variables <?php $a1 = "WILLIAM"; $a2 = "henry"; $a3 = "gatES"; Returning Global Variables

  11. Local variables are accessible just from the part of code where you define them • Global variables are accessible from all parts of your code • Static variables are accessible only within the function that declared them but retain their value over multiple calls Recap of Variable Scope

  12. Using include, you can tell PHP to fetch a particular file and load all its contents • Using include_once • Using require and require_once The include Statement

  13. PHP Version Compatibility Example 5-9. Checking for a function’s existence <?php if (function_exists("array_combine")) { echo "Function exists"; } else { echo "Function does not exist - better write our own"; } ?>

  14. You need to design a composite of data and code called a class. Each new object based on this class is called an instance (or occurrence) of that class Terminology

  15. Before you can use an object, you must define a class with the class keyword • Example 5-10. Declaring a class and examining an object • Creating an Object • Accessing Objects • Example 5-11. Creating and interacting with an object Declaring a Class

  16. 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. Constructors

  17. Example 5-19. Defining a property implicitly <?php $object1 = new User(); $object1->name = "Alice"; echo $object1->name; class User {} ?> Declaring Properties

  18. In the same way that you can create a global constant with the define function, you can define constants inside classes. The generally accepted practice is to use uppercase letters to make them stand out, as in Example 5-21. Declaring Constants

  19. Use public when outside code should access this member and extending classes should also inherit it. • Use protected when outside code should not access this member but extending classes should inherit it. • Use private when outside code should not access this member and extending classes also should not inherit it • Example 5-22. Changing property and method scope Property and Method Scope in PHP 5

  20. Once you have written a class, you can derive subclasses from it. • In Example 5-24, the class Subscriber is declared a subclass of User by means of the extends operator. Inheritance

More Related