chapter7PHPTUT-nicephotog

download chapter7PHPTUT-nicephotog

of 1

Transcript of chapter7PHPTUT-nicephotog

  • 8/9/2019 chapter7PHPTUT-nicephotog

    1/1

    (Chapter 7) PHP Objects (Code optimising and classes)

    Programming Objects (OOP)

    First , object oriented programming is a method of constructing a program. An object itself is more of a concept thanan actual data type of a programming language. An object for most languages is a device with can be made packetsof code with sets of procedures/methods/functions that relate their class or object name, of the class or objectname, and relates the general purpose of the packeted code sets.PHP objects are called classes.and they have an ability to be extended similar to the purpose and other classesthat Java2 or C++ has of abstract classes. Again a PHP class may or may not have a special function-like systemcalled a constructor.

    Basic PHP class system

    Aside to similarities of other languages (if they help you) PHP has its own layout of these systems and associate operatorsfor handling objects and one or two functions designed specifically for PHP operations with objects.To refer to a class that has not been instantiated (created) an operator:: (double colon) is used between the class nameto be called(as instance in runtime code) and the function to be called from that class.In an extended class the parent class can be called using the parent keyword in front of the :: double colon operator.To refer to an instance of a variable inside a class and call a function to set it when the variable could be existentin other copies of the class also existent at the time, the variable is preceded by a special variable and symbols $this->e.g. $this->browser_name = get_browser().Variables for the class declared in the global scope of the class also require the var keyword preceding themand are not given a setting but declared with no value and no equal sign.Normally this condition causes a warning from theruntime parser if there is no assigned value to a variable but not with the internal global variable of a class. To instantiatea variable of a class a constructor function must be used or the variables value can be set by calling a function inside theclass that is designed to either return the variable with a value(e.g. numeric) or assign it(e.g. string).To create a substantiated individual reference of a class requiresusing a variable and the new keyword.Some names for functions are reserved in PHP for special purposes and cannot be used by the programmer except fortheir designed purpose e.g. _sleep and _wakeup or serialize() and unserialize() object copy passing. This last set ofactions is a little complex here and also the & operator for reference copies of objects and parts that operates a littlesimilar to C/C++ or Fortran pointers so they will not be explained here in this tutorial.