
|
|
|||
What is Object Oriented Programming (OOP)?By Tony Marston2007-01-30
The difference between OOP and non-OOP A better way of trying to explain the differences between non-OO and OO programming is to actual examples. They are defined differentlyA function is defined as a self-contained block of code. Each function name "fName" must be unique within the application.function fName ($arg1, $arg2) A class method is defined within the boundaries of a class definition. Each class name "cName" must be unique within the application. Each class may contain any number of functions (also known as "methods"), and the function name "fName" must be unique within the class but need not be unique within the application. class cName They are accessed differentlyIt is important to note that neither a function nor a class can be accessed until the function/class definition has been loaded.Calling a function is very straightforward: $result = fName($arg1, $arg2); Calling a class method is not so straightforward. First it is necessary to create an instance of the class (an object), then to access the function (method) name through the object. The object name must be unique within the application. $object = new cName; They have different numbers of working copiesA function does not have to be instantiated before it can be accessed, therefore only one copy (or instance) is said to exist at any one time.A class method can only be accessed after it has been instantiated into an object, and it is possible to create multiple instances (objects) of the same class with different object names. $object1 = new cName; They have different numbers of entry pointsA function has only a single point of entry, and that is the function name itself.An object has multiple points of entry, one for each method name. They have different methods of maintaining stateA function by default does not have state, by which I mean that eachtime that it is called it is treated as a fresh invocation and not a continuation of any previous invocation. An object does have state, by which I mean that each time an object's method is called it acts upon the object's state as it was after the previous method call. It is possible for both a function and a class method to use local variables, and they both operate in the same way. This means that the local variables do not exist outside the scope of the function or class method, and any values placed in them do not persist between invocations. It is possible for a function to remember values between different invocations by declaring a variable as static, as in the following example:
function count () {
Each time this function is called it will return a value that is one greater than the previous call. Without the keyword static it would always return the value '1'.
Class variables which need to persist outside of a function (method) are declared at class level, as follows: class calculator Note that all class/object variables are referenced with the prefix $this-> as in $this->varname. Any variable which is referenced without this keyword, as in $varname, is treated as a local variable.
Note also that each instance of the class (object) maintains its own set of variables, so the contents of one object are totally independent of the contents of another object, even it is from the same class. Tutorial Pages: » Introduction » What OOP is NOT » What is an Object Oriented language? » What OOP is » The difference between OOP and non-OOP » Practical Examples » Conclusion » References |
||||
| About the NetVisits, Inc Network | Write For Us | Advertise Copyright ©2007 NetVisits, Inc Network. All Rights Reserved. Privacy Policy. |
Visit other NetVisits, Inc. sites: |