Page 182 - Open Soource Technologies 304.indd
P. 182

Web Technologies-I



                   Notes         If you are familiar with JavaScript, it should be obvious by now that the -> operator plays the
                                 same role in PHP as the dot operator in JavaScript. For example, in JavaScript, this produces
                                 the date for Christmas Day 2011:

                                 var xmas2011 = new Date(2011, 11, 25); // months are zero-based alert(‘Christmas 2011 is on
                                 ‘ + xmas2011.toString());

                                 JavaScript also uses the dot operator for properties:
                                 objectName.propertyName
                                 If you are familiar with the basics of OOP in PHP, you should recognize that you often access
                                 class data and functionality through objects. For instance, you should be familiar with the syntax:
                                 $Foo = new Foo();

                                 $Foo->Name = “Navneet”;
                                 The above snippet alters the  public member variable  Name in class Foo. However, with  the
                                 introduction of the static keyword in PHP 5, we can now access methods and properties through
                                 the context of a class rather than only the object (note: these methods/properties need to be declared
                                 static first).



                                 class Foo {
                                 static public $Name = “Pradip kumar”;

                                 static public function helloWorld() {
                                  print “Hello world from “ . self::$Name;
                                   }

                                 }
                                 To declare a method or property as static, we must use the static keyword. When accessing
                                 properties from outside the class scope, we use the class name followed by two: and then the
                                 property name. For instance:

                                 print Foo::$Name . “\n”;
                                 Foo::helloWorld();

                                 The above would output:
                                 Pradip kumar
                                 Hello world from Pradip kumar

                                 As static methods are within the class scope, they cannot access any normal methods or properties
                                 (i.e. those that have not been declared static), as those would belong to the object, and not the
                                 class. A by-product of this is you cannot use the $this variable from within a static method, as
                                 static methods are not invoked in the context of an object.

                                                You should be able to see that from within the context of the class, to access
                                                member variables or properties you use the self keyword.








        176                               LOVELY PROFESSIONAL UNIVERSITY
   177   178   179   180   181   182   183   184   185   186   187