Chapter three Syntax 5, functions and classes

Source: Internet
Author: User
Tags include variables php class require reserved
Functions | syntax

Function

In PHP, programmers are allowed to organize components such as commonly used processes or variables into a fixed format. That is, users can assemble functions or classes themselves.

Functions in PHP, like the C language, include return value  and no return value , not as complex as Pascal partitioning functions (function) and programs (procedure).

On the name of the function, PHP is loosely controlled for capitalization. You can write a capital name when you define a function, and use a lowercase name when you use it. In short, for functions, regardless of the case, as long as the attention of the name does not repeat the good.

Here is the syntax for using the function


function MyFunc ($arg _1, $arg _2, ..., $arg _n) {
Perform some steps
return $retval;
}

When used, the reserved word of a function is added before the custom function name, which means that it defines a user-defined function. After the myfunc can be any letter at the beginning of the string, the string except the beginning can not be a number or underscore, after the first letter can be Arabic numerals or underscores, of course, other symbols or Chinese characters can not be used as function names.

$arg _1 to $arg _n the parameters used by the function, separated by commas. The curly brace {} After the argument is the part of the entire function. function returns a value if the value is returned by using return. Parameters can define initial or default values beforehand. A defined default value  parameter can be omitted when using a function, but it must be placed after the default value parameter is not configured, otherwise PHP will have an error parsing the function.

The other is the type of parameter, as long as the parameter is a variable type supported by PHP, whether it is an array, a string, or an integer .... Wait a minute. The return value is the same.

The following is an example of using default values and  default values

<?php
function myfunc1 ($arg _1, $arg _2, $arg _3= "I am the default string") {
echo $arg _1+ $arg _2;
echo $arg _3. " <p>\n ";
}

MyFunc (3, 4); Parameter $arg _3 omitted.
MyFunc (6, 6, "No default Value")//input parameter $arg _3.
?>


The value of the parameter, which is usually entered using a  method, and sometimes a pointer to a parameter can be passed in by using the address in the way that is worth it. The method is to precede the parameter with the & symbol. The following example

<?php
Function Myfunc2 (& $argstr) {
$argstr =ereg_replace ("/", "-", $argstr);
}

$today = "2000/01/01";
MYFUNC2 ($today);
Echo $today; 2000-01-01;
?>


Class

User-defined classes are also one of the prerequisites for learning PHP well. And the PHP class, and other object-oriented language comparison, is quite simple. PHP has only categories (Class), methods (method), attributes, and single inheritance (extensions). For users who are not accustomed to using C + +, Java, Delphi and other object-oriented language to develop programs, it may be useful to read a book on object-oriented concepts, which can bring a lot of gains.

The following example is a trolley  class. As you can see, using class indicates that it is a class category. A function in a category, such as Add_item, represents a method of that class. Methods can encapsulate the actual processing of a class, allowing the class to perform some steps in a packaged way.

The $this class variable in the program is also the same as $GLOBALS and $php _errormsg two variables, which are special variables in PHP. $this variables are used only in class classes to represent the class itself.

<?php
Program Name: Cart.inc
Class Cart {
var $items; Trolley type

This method adds $num item to the trolley (added to the $ARTNR variable)
function Add_item ($ARTNR, $num) {
$this->items[$artnr] + = $num;
}

This method reduces $num items from trolleys (from $ARTNR variable)
function Remove_item ($ARTNR, $num) {
if ($this->items[$artnr] > $num) {
$this->items[$artnr]-= $num;
return true;
} else {
return false;
}
}
}
?>


A trolley can be used in a similar way as the following example. You can save each class as an include file, and then require or include it in. When you define a variable $cart, you use the reserved word new to indicate that $cart use the cart class. Use the-> symbol to represent the method that executes the class.

<?php
Require ("Cart.inc");
$cart = new Cart;
$cart->add_item ("10", 1);
?>


Then a registered trolley is designed. Hand carts are inherited from trolleys, so the methods and properties owned by trolleys are also available, and the method by which a named trolley adds a name to a trolley (perhaps a more appropriate attribute).

As you can see in the following example, subclasses Named_cart use extends to inherit their parent Cart. Although there is no way to add items and reduce items in the Named_cart category, there are things that the parent has as a result of genetic characteristics.

<?php
Program Name: Named_cart.inc
Require ("Cart.inc");
Class Named_cart extends Cart {
var $owner;
function Set_owner ($name) {
$this->owner = $name;
}
}
?>


To use a hand trolley class, see the example below. Of course, this is not a good design, each subclass has been require its parent class, will cause the server on the I/O burden. In practice, the entire series of classes can be in the same program file, from the earliest Joe to the last descendants, and also for future revisions.

<?php
Require ("Named_cart.inc");
$ncart = new Named_cart; Building class variables
$ncart->set_owner ("Cyberridder"); The named property of the configuration class
Echo $ncart->owner; To display a class's named property
$ncart->add_item ("10", 1); Methods inherited from the parent can also be used
?>


So, with the extends reserved word in PHP, combined with a good system analysis and a complete CRC card (see object-oriented Books), PHP becomes a CGI language with a powerful class of capabilities.

PHP is a scripting language (script), so the program source code is visible, the component black box in software engineering will not appear in the current PHP version, that is, all classes do not actually hide its contents. For the software industry, there is no way to protect the so-called software IC, standing in the open group, instead of the source code is a good thing, as for what is not, it is difficult to determine, but the current PHP or open Source group, perhaps in the future Zend engine can do class encapsulation function is not necessarily.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.