20
PHP Functions and Objects Chapter 5

PHP Functions and Objects

  • Upload
    frisco

  • View
    42

  • Download
    0

Embed Size (px)

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

Page 1: PHP Functions and Objects

PHP Functions and ObjectsChapter 5

Page 2: PHP Functions and Objects

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

Page 3: PHP Functions and Objects

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.

Page 4: PHP Functions and Objects

Objects

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

Page 5: PHP Functions and Objects

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

Page 6: PHP Functions and Objects

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

?>

Page 7: PHP Functions and Objects

Defining a function

• The general syntax for a function is:

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

{

// Statements

}

Page 8: PHP Functions and Objects

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

Page 9: PHP Functions and Objects

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);

}

?>

Page 10: PHP Functions and Objects

Returning Global Variables

• Example 5-5. Returning values in global variables

<?php

$a1 = "WILLIAM";

$a2 = "henry";

$a3 = "gatES";

Page 11: PHP Functions and Objects

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

Page 12: PHP Functions and Objects

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

Page 13: PHP Functions and Objects

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

}

?>

Page 14: PHP Functions and Objects

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

Page 15: PHP Functions and Objects

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

Page 16: PHP Functions and Objects

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.

Page 17: PHP Functions and Objects

Declaring Properties

• Example 5-19. Defining a property implicitly

<?php

$object1 = new User();

$object1->name = "Alice";

echo $object1->name;

class User {}

?>

Page 18: PHP Functions and Objects

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.

Page 19: PHP Functions and Objects

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

Page 20: PHP Functions and Objects

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.