Low cost ecommerce web development India flash website design

 

Understanding Scope

Write more than a few hundred lines of procedural PHP code and, no doubt,

you'll run into a parser error or, worse still, a mysterious bug caused by your accidentally

having used a function or variable name more than once. When you're

including numerous files and your code grows increasingly complex, you may

find yourself becoming more paranoid about this issue. How do you stop such

naming conflicts from occurring? One approach that can help solve this problem

is to take advantage of scope to hide variables and functions from code that

doesn't need them.

A scope is a context within which the variables or functions you define are isolated

from other scopes. PHP has three available scopes: the global scope, the function

scope, and the class scope. Functions and variables defined in any of these

scopes are hidden from any other scope. The function and class scopes are local

scopes, meaning that function X's scope is hidden from function Y's scope, and

vice versa.

The big advantage of classes is that they let you define variables and the functions

that use them together in one place, while keeping the functions hidden from

unrelated code. This highlights one of the key theoretical points about the object

oriented paradigm. The procedural paradigm places most emphasis on functions,

variables being treated as little more than a place to store data between function

calls. The object oriented paradigm shifts the emphasis to variables; the functions

“back” the variables and are used to access or modify them.

Let's explore this through an example:

<?php

// A global variable

$myVariable = 'Going global';

// A function declared in the global scope

function myFunction()

{

// A variable in function scope

$myVariable = 'Very functional';

}

// A class declared in the global scope

class MyClass {

// A variable declared in the class scope

var $myVariable = 'A class act';

In the above example, each of the $myVariable declarations is actually a separate

variable. They can live together happily without interfering with each other, as

each resides in a separate scope. Similarly, the two myFunction declarations are

two separate functions, which exist in separate scopes. Thus PHP will keep all

of their values separate for you.

Scope becomes important when you start to use object oriented programming in

a significant way in your PHP applications. As many classes can have methods

of the same name, you can design separate classes to deliver the same application

programming interface (API). The scripts that use the classes can then use the

same method calls, irrespective of which class was used to instantiate the object

they're working with. This can be a very powerful technique in writing maintainable

code.

website designer freelance ASP PHP ecommerce web developer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110