PHP Reusing Code and Writing Functions 1 Reusing

  • Slides: 10
Download presentation
PHP Reusing Code and Writing Functions 1

PHP Reusing Code and Writing Functions 1

Reusing Code. Functions. Topics: Code inclusion using require() and include() Defining functions Managing function

Reusing Code. Functions. Topics: Code inclusion using require() and include() Defining functions Managing function parameters Passing-by-reference vs. passing-by-value Parameters default values Variable-length parameter lists Understanding variable scope in functions Using functions to return values Using recursive functions Anonymous functions 2

Reusing Code - why? Advantages: Reduces costs Reusing eliminates time spent to design, code,

Reusing Code - why? Advantages: Reduces costs Reusing eliminates time spent to design, code, test, debug a new piece of software. Increases reliability Existing, mature code is usually more reliable than new code: it has already been thoroughly tested, possible defects have been discovered during testing (and use), and have been fixed. Improves consistency Existing code is already consistent with the other parts of the system: in terms of user interfaces, interfaces with other systems. 3

Modularity - why? … has a favorable impact on program development: Allows program to

Modularity - why? … has a favorable impact on program development: Allows program to be divided into logical pieces: Level of difficulty grows with the size and complexity of a program Divide work among programmers Easier to debug when modules are tested alone fully first, then integrated systematically Easier to read, especially when commented correctly. Easier to modify, isolating modules to change Allows for reuse (avoiding redundant code); when an algorithm or computation is done over and over, it can be put in a method. 4

Code Inclusion PHP allows to reuse any type of code (not only functions, classes):

Code Inclusion PHP allows to reuse any type of code (not only functions, classes): Can insert code from a file into your PHP script with include (‘filename’); require (‘filename’); Statements include() & require() are similar, except when they fail: include() construct just gives a warning if failing to include file require() construct gives a fatal error, that will cause program to terminate Can also use variations include_once() or require_once() to avoid problems with redundancy; ex. redefining same function Slower than include()/require() 5

Fail_include. php http: //www. nku. edu/~frank/csc 301/Examples/PHP_Functi ons/fail_include_php. pdf http: //www. nku. edu/~frank/csc 301/Examples/PHP_Functi

Fail_include. php http: //www. nku. edu/~frank/csc 301/Examples/PHP_Functi ons/fail_include_php. pdf http: //www. nku. edu/~frank/csc 301/Examples/PHP_Functi ons/fail_include. php

Code Inclusion The files loaded using include / require can contain everything that is

Code Inclusion The files loaded using include / require can contain everything that is normally used in a PHP file: PHP statements, PHP functions, PHP classes, HTML code, client-side scripting PHP code still has to be placed within PHP tags Filename extensions: When include()/require() is used in a PHP script: the loaded file becomes part of the PHP script and is executed as such → as if the loaded file’s contents replaced the include()/require() statement PHP does not look at the filename extension of an included file include files can have any extension, if not to be used directly. Usually: . php or. inc (note: if located in the web document tree, their content can be seen in clear!) 7

Main. php http: //www. nku. edu/~frank/csc 301/Examples/PHP_Functi ons/main_php. pdf http: //www. nku. edu/~frank/csc 301/Examples/PHP_Functi

Main. php http: //www. nku. edu/~frank/csc 301/Examples/PHP_Functi ons/main_php. pdf http: //www. nku. edu/~frank/csc 301/Examples/PHP_Functi ons/reusable_php. pdf http: //www. nku. edu/~frank/csc 301/Examples/PHP_Functi ons/main. php

Home. html http: //www. nku. edu/~frank/csc 301/Examples/PHP_Functi ons/home. php http: //www. nku. edu/~frank/csc 301/Examples/PHP_Functi

Home. html http: //www. nku. edu/~frank/csc 301/Examples/PHP_Functi ons/home. php http: //www. nku. edu/~frank/csc 301/Examples/PHP_Functi ons/home_php. pdf http: //www. nku. edu/~frank/csc 301/Examples/PHP_Functi ons/header_php. pdf http: //www. nku. edu/~frank/csc 301/Examples/PHP_Functi ons/footer_php. pdf

Code Inclusion The include files can be dynamic and generate on-the-fly parts of the

Code Inclusion The include files can be dynamic and generate on-the-fly parts of the page Apache or PHP can be configured to automatically load specific pages / files before and after every page, even for individual directories (applications) include() statements not needed in this case Not on cscdb… 10