Metropolitan State University

ICS 325

Internet Application Development

 

Class Notes – Chapter 6 – Object-Oriented PHP

 

Object-Oriented Programming (OOP) Concepts

 

ADT –                         Abstract Data Type

Objects –                    An implementation of a class

Classes –                   Programming constructs that allow programmers to implement real world things.

Encapsulation –        refers to a form of protected aggregation. Data is said to be encapsulated in a class if the data cannot be directly accessed.

Polymorphism –        refers to a programming language's ability to process objects differently depending on their data type or class.

Inheritance –              refers to the notion of deriving one class from another which enables different classes to have a consistent interface

 

Creating Classes, Attributes, and Operations in PHP

Structure –

            Start the class structure using the key word “class”

            Give the class an appropriate name

            Endclose the class with opening and closing curly braces

            class className{

}

 

Class Attributes

            Use keyword “var” to declare all variables used in the class

            All class variables are denoted with the $ sign

            All class attributes should have a meaningful name

            Only class attributes declared are available for use when the object is created.

            The use of “this” keyword ensures that the calling class uses the correct attribute.

            class className{

        var $myVar1;

        $myVar1 = 10;

        $this->myVar1 = 20;

}

 

Class Operations

            Class operations are the same as functions in PHP.

A class function’s name should reflect the task that it implements.

            All class functions should perform one task well.

            A class function should not be more than a page long.

            class className{

        function myFunc($param1,$param2){

  }

}

 

Constructor

            A constructor is called when the object is being created

            A constructor is the same as all other class functions, except it is named after the class

            Most often a constructor is used to initialize, and give value to attributes in an object

class className{

        function className(){

  }

}

Destructor

            PHP does not have a destructor

            Garbage collection is handle by PHP,

 

Instantiation of a class

                        After declaring a class we need to create, or instantiate, a new object with the keyword “new”.

                        When creating a new object based on a class the default constructor is called.

                        Store the new object into a PHP variable.

 

class className{

        function className(){

            // do something;

  }

}

$myVar1 = new className();

$myVar2 = new className();

 

Encapsulation

Encapsulation is not directly supported in PHP.

Encapsulation should implemented by the programmer.

By creating get and post methods for variables, and a little discipline encapsulation can be implemented.

 

class className{

  var $value;

 

        function setValue($inValue){

            $this->value = $inValue;                 

  }

        function getValue(){       

            return $this->value;

  }

}

Inheritance

For one class to inherit from another class, in PHP, the keyword “extends” should be used.

See next section for examples.

Multiple inheritance is not supported in PHP

 

Polymorphism/Overriding

Polymorphism is implemented in PHP

If many classes have the same member function name, at runtime you can control which object’s function gets called based on the object’s type.

 

class classPoly{       

        function poly($x){

            $x->polyFunc()

  }

}

 

class classOne{

        function polyFunc(){

            echo “do something”                

  }

}

 

                        class classTwo{

  function polyFunc(){

            echo “do something else”;                

  }

}

$one = new classOne();

$two = new classTwo();

$objPoly = new classPoly();

objPoly->poly($one);          // do something

objPoly->poly($two);          // do something else

Overriding a function is not directly supported in PHP

            The developer can implement overriding of functions based on the variables passed in.

            A test for variable type of number of passed in parameters can be used to then call another function

Here is a list of built in functions available in PHP for testing variables.
is_array()                                            is_bool()
is_callable()                                       is_double()
is_float()                                             is_int()
is_integer()                                        is_long()
is_null ()                                              is_numeric()
is_object()                                          is_real()
is_resource()                                     is_scalar()
is_string()       To get the number of parameters in a function use the following built in PHP functions
func_num_args()

           

            class classOne{

              var $numArgs;

              var $arrArgs;

              function myFunc(){

            $numArgs = func_num_args();

            $arrArgs = gunc_get_args();

            if ($numArgs == 1){

              if (is_string($arrArgs[0])){

                  strArg($arrArgs[0]);

              }

              else{

intArg($arrArgs[0]);

  }

}

else{

  mulArg($arrArgs[0],$arrArgs[1]);

}

  }

  function strArg($arg1){}

                    function intArg($arg1){}

  function mulArg($arg1,$arg2){}

}