PHP Functions and Objects

Preview:

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

PHP Functions and ObjectsChapter 5

PHP Functions

• 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

Why Function Junction?

• 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.

Objects

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

PHP Functions

• Hundreds of ready-made, built-in functions

print("print is a function");

The parentheses tell PHP that you’re referring to a function

Builtin Functions

• Functions can take any number of arguments, including zero

• Example 5-1. Three string functions

<?php

echo strrev(" .dlrow olleH"); // Reverse string

echo str_repeat("Hip ", 2); // Repeat string

echo strtoupper("hooray!"); // String to uppercase

?>

Defining a function

• The general syntax for a function is:

function function_name([parameter [, ...]])

{

// Statements

}

Returning a Value

• 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 an Array

• 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 Global Variables

• Example 5-5. Returning values in global variables

<?php

$a1 = "WILLIAM";

$a2 = "henry";

$a3 = "gatES";

Recap of Variable Scope

• 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

The include Statement

• Using include, you can tell PHP to fetch a particular file and load all its contents

• Using include_once• Using require and require_once

• 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";

}

?>

Terminology

• 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

Declaring a Class

• 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

Constructors

• 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.

Declaring Properties

• Example 5-19. Defining a property implicitly

<?php

$object1 = new User();

$object1->name = "Alice";

echo $object1->name;

class User {}

?>

Declaring Constants

• 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.

Property and Method Scope in PHP 5

• 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

Inheritance

• 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.