Php Tutorial 21-30/46

Php Tutorial


  1. PHP is a script language and interpreter that is freely available and used primarily on Linux Web servers. PHP, originally derived from Personal Home Page Tools, now stands for PHP: Hypertext Preprocessor, which the PHP FAQ describes as a "recursive acronym.


Name Code

Write to File


  < ? php
     $filename = 'test.txt';
     $somecontent = "Add this to the file\n";
     // Let's make sure the file exists and is writable first.
     if (is_writable($filename))
     {
       // In our example we're opening $filename in append mode.
       // The file pointer is at the bottom of the file hence
       // that's where $somecontent will go when we fwrite() it.
       if (!$handle = fopen($filename, 'a'))
       {
         print "Cannot open file ($filename)";
         exit;
       }
       // Write $somecontent to our opened file.
       if (!fwrite($handle, $somecontent))
       {
         print "Cannot write to file ($filename)";
         exit;
       }
       print "Success, wrote ($somecontent) to file ($filename)";
       fclose($handle);
     }
     else
     {
       print "The file $filename is not writable";
     }
  ? > 

Finding a random line of a file


  < ? php
     function randomint($max = 1)
     {
       $m = 1000000;
       return ((mt_rand(1,$m * $max)-1)/$m);
     }
     ?>
     $line_number = 0;
     $fh = fopen('data.txt','r') or die($php_errormsg);
     while (! feof($fh))
     {
       if ($s = fgets($fh))
       {
         $line_number++;
         if (randomint($line_number) < 1)
         {
           $line = $s;
         }
       }
     }
     fclose($fh) or die($php_errormsg);
  ? > 

A Class with a Constructor


  < ? php
     class Item
     {
       var $name;
       function Item( $name="item")
       {
         $this->name = $name;
       }
       function setName( $n)
       {
         $this->name = $n;
       }
       function getName ()
       {
         return $this->name;
       }
     }
     $item = new Item("5");
     print $item->getName ();
  ? > 

Class Inheritance


  < ? php
     class ParentClass
     {
       public $parentvar;
       public function parentOne()
       {
         echo "Called parentOne()\n";
       }
       private function parentTwo()
       {
         echo "Called parentTwo()!\n";
       }
     }
     class ChildClass extends ParentClass
     {
       public function childOne()
       {
         echo "Called childOne()!\n";
       }
     }
     $v = new ChildClass();
     $v->parentOne();
  ? >        

Comparing Objects with == and ===


  < ? php
     class Employee { }
     $Bob = new Employee( );
     $Joe = clone $Bob;
     print (int)($Bob == $Joe) . "\n";
     print (int)($Joe === $Joe) . "\n";
     class Employee
     {
       public function __construct( )
       {
         $this->myself = $this;
       }
     }
     $Bob = new Employee( );
     $Joe = clone $Bob;
     print (int)($Bob == $Joe) . "\n";
     print (int)($Bob === $Joe) . "\n";
  ?> 

Create Class instance


  < ? php
     class ClassName
     {
       var $var1;
       var $var2;
       function myfunction1()
       {
         $result = "$this->var1 and $this->var2";
         return $result;
       }
       function myfunction2()
       {
         $myfunction1 = $this->myfunction1();
         return $myfunction1;
       }
     }
     $myclass = &new ClassName;
     $myclass->var1 = "A";
     $myclass->var2 = "B";
     echo $myclass->myfunction1()."
";
     echo $myclass->myfunction2()."
";
  ? > 

Extends and Implements


   < ? php
     class Employee
     {
       private $name;
       function setName($name)
       {
         if ($name == "")
           echo "Name cannot be blank!";
         else
           $this->name = $name;
       }
       function getName()
       {
         return "My name is ".$this->name."
";
       }
     }
     interface MyInterface
     {
       function account();
       function doc();
     }
     class Executive extends Employee implements MyInterface
     {
       private $totalStockOptions;
       function account()
       {
         echo "account";
       }
       function doc()
       {
         echo "doc";
       }
     }
     class Assistant extends Employee implements MyInterface
     {
       function takeMemo()
       {
         echo "memo";
       }
       function account()
       {
         echo "memo";
       }
       function doc()
       {
         echo "Start small fire in the trash can.";
       }
     }
  ?> 

Inheriting a Shape Class


  < ? php
     class shape
     {
       var $x;
       var $y;
       function shape()
       {
         print("Shape constructor called
");
       }
       function get_x()
       {
         return $this->x;
       }
       function get_y()
       {
         return $this->y;
       }
       function set_x($x)
       {
         $this->x = $x;
       }
       function set_y($y)
       {
         $this->y = $y;
       }
       function move_to($x, $y)
       {
         $this->x = $x;
         $this->y = $y;
       }
       function print_data()
       {
         print("Shape is currently at " . $this->get_x() . ":" .
       $this->get_y());
       }
       function draw()
       {}
     }
     class rectangle extends shape
     {
       function rectangle($x, $y)
       {
         $this->move_to($x, $y);
       }
       function draw()
       {
         print("Drawing rectangle at " . $this->x . ":" .
           $this->y);
       }
       function print_data()
       {
         print("Rectangle currently at " . $this->get_x() . ":" .
           $this->get_y());
       }
     }
     $rect1 = new rectangle(100, 100);
     $rect1->draw();
     $rect1->print_data();
  ?> 

Object Overloading


  < ? php
     class Data
     {
       private $data = array();
       public function __set($name, $value)
       {
         $this->data[$name] = $value;
       }
       public function __get($name)
       {
         if (isset($this->data[$name])) { return $this->data[$name]; }
       }
       public function __isset($name)
       {
         return isset($this->data[$name]);
       }
       public function __unset($name)
       {
         unset($this->data[$name]);
       }
     }
     $data = new Data();
     $data->name = 'F';
     echo "The data value of 'name' is {$data->name}";
     unset($data->name);
     echo 'The value is ', isset($data->name) ? '' : 'not ', 'set.';
  ? > 

Object Type Information


  < ? php
     class Dog
     {
       public $Name;
       private function getName( )
       {
         return $this->Name;
       }
     }
     class Poodle extends Dog
     {
       public function bark( )
       {
         print "'Woof', says " . $this->getName( );
       }
     }
     $poppy = new Poodle;
     $poppy->Name = "Poppy";
     $poppy->bark( );
     if ($poppy instanceof poodle) { }
     if ($poppy instanceof dog) { }
  ?> 
Php Tutorial 21-30/46 Php Tutorial 21-30/46 Reviewed by Abdul hanan on 10:45:00 Rating: 5
Powered by Blogger.