Posts

Showing posts from October, 2010

PHP: setters and getters

/* * PHP: Using setters and getters * This clsss will show the usage of PHP setters and getters * */ class Employee { //This is private, so this can't be accessed from out side the class. private $name = "Dantha Elvitigala"; //public function.this can be access from outside the class //it's used to set the employee name into variable $name public function setName($name){ $this->name= $name; } //public function.this also can be access from outside the class //this use for get the emploee name stored in $name public function getName(){ return $this -> name; } }//end class //create an instant of the Employee class $employee = new Employee(); //set $name with another employee name $employee -> setName("Chandika Elvitigala"); // printout the employee name echo $employee->getName();

PHP arrays

* In PHP, there are three kind of arrays: * 1)Numeric array - An array with a numeric index * 2)Associative array - An array where each ID key is associated with a value * 3)Multidimensional array - An array containing one or more arrays 1)Numeric Arrays A numeric array stores each array element with a numeric index. There are two methods to create a numeric array. 1. In the following example the index are automatically assigned (the index starts at 0): $array1 = array(3,5,7,2); echo $array1[0]." "; $array2 = array(4,"dantha" ,"brown" ,array("x","y","z")); echo $array2[3][2]." "; $array2[3] = "Cat"; echo $array2[3]." "; 2. In the following example we assign the index manually: $cars[0]="Honda"; $cars[1]="Nissan"; $cars[2]="BMW"; $cars[3]="Toyota"; echo $cars[0]." and ".$cars[1]." are Japanese cars"; 2) ASSOCIAT

PHP Static modifiers

<?php // static modifier // with static methods you can't use $this // this : talking about current instance // self : talking about current class class Student{ static $total_students = 6; static public function add_students() { //here you can use either Student or self. //but self is better because it's inside the Student class. self::$total_students++;// self : talking about current class. } static function welcome_students($var="Hello") { echo "{$var} students"; } } //$student = new Student(); //$student-> total_students; // <- instance call echo Student::$total_students." "; // <- static call echo Student::welcome_students("Good bye")." "; echo Student::add_students()." "; //Student::$total_students = 1; echo Student::$total_students." "; /* -------- VERY IMPORTANT ---------- * static variables are shared throughout the inheritance tree * even if