PHP/Class/Class Method

Материал из Web эксперт
Перейти к: навигация, поиск

Accessing the Attributes of a Class by Using Functions

   <source lang="html4strict">

<?php

 class employee{
     var $emp_code;
     var $name;
     var $address;
     var $department;
     var $sex;
     var $date_of_birth;
     var $salary;
     function showsalary () {
        echo "Your salary is :",$this->salary," \n";
     }
 }
 $dave = new employee;
 $dave->emp_code="8";
 $dave->name="Dave";
 $dave->address="Apartment 1";
 $dave->department="Admin Development";
 $dave->sex="Male";
 $dave->salary="70";
 $dave->date_of_birth="15-09-1977";
 $dave->showsalary();

?>

      </source>
   
  


Access properties

   <source lang="html4strict">

<?php

 class Bird
 {
   function __construct($name="No-name", $breed="unknown", $price = 15)
   {
     $this->name = $name;
     $this->breed = $breed;
     $this->price = $price;
   }    
   
   function setName($name)
   {
     $this->name = $name;
   }    
   
   function setBreed($breed)
   {
     $this->breed = $breed;
   }    
   
   function setPrice($price)
   {
     $this->price = $price < 0 ? 0 : $price;
   }
   
   function getName()
   {
     return $this->name;
   }    
   
   function getBreed()
   {
     return $this->breed;
   }    
   
   function getPrice()
   {
     return $this->price;
   }
   function display()
   {
printf("

%s is a %s, and costs \$%.2f.

\n",
             $this->name, $this->breed, $this->price);
   }
 }
 
 $magpie = new Bird("Malaysia", "magpie", 7.5);
 $magpie->display();
 
 $magpie->setPrice(-14.95);
 $magpie->display();
 
 $magpie->price = -14.95;
 
 $magpie->display();

?>

 </source>
   
  


A Class with a Method

   <source lang="html4strict">

<?php class Item {

 var $name = "item";
 function getName() {
   return "item";
 }
}
$item = new Item ();
print $item->getName ();

?>

 </source>
   
  


Call class methods directly

   <source lang="html4strict">

<?php

  class Visitors
  {
     public function greetVisitor()
     {
        echo "Hello
"; } function sayGoodbye() { echo "Goodbye
"; } } Visitors::greetVisitor(); $visitor = new Visitors(); $visitor->sayGoodbye();

?>

      </source>
   
  


Calling an Overridden Method (PHP 5 Syntax)

   <source lang="html4strict">

<?php

class Item {
  private $name;
  function __construct( $name="item", $code=0 ) {
    $this->name = $name;
    $this->code = $code;
  }
 function getName() {
   return $this->name;
 }

} class PriceItem extends Item {

 function getName() {
   return "(price) ".parent::getName ();
 }

} $item = new PriceItem ("widget", 5442); print $item->getName(); ?>

 </source>
   
  


Class Member and Method Definitions

   <source lang="html4strict">

Name Description Const Defines a constant member. Public Accessible from any object of the class. Protected Accessible from the class where it is defined and from inherited classes. Private Accessible from the class where it is defined. Static Modifier. When used alone, public is assumed. <?php class myclass {

   public $a; 
   function set_value($val) { 
       $this->a = $val; 
   } 

} $obj = new myclass; $obj->set_value(123); echo "Member a = $obj->a\n"; $obj->a = 7; echo "Member a = $obj->a\n"; ?>

 </source>
   
  


Class Member Overloading

   <source lang="html4strict">

<?php

    class ParentClass {
         public function callMe() {
              echo "Parent called!\n";
         }
    }
    class ChildClass extends ParentClass {
         public function callMe() {
              echo "Child called!\n";
         }
    }
    $child = new ChildClass;
    $child->callMe();

?>

 </source>
   
  


Defining three member functions for Cat

   <source lang="html4strict">

<?php Class Cat {

 // Constructor
 function __constructor(  ) {
 }
 // The cat meows
 function meow(  ) {
   echo "Meow...";
 }
 // The cat eats
 function eat(  ) {
   echo "*eats*";
 }
 // The cat purrs
 function purr(  ) {
   echo "*Purr...*";
 }

} ?>

 </source>
   
  


Overriding Methods

   <source lang="html4strict">

<?

   class Dog {
           public function bark( ) {
                   print "Woof!\n";
           }
   }
   class Poodle extends Dog {
           public function bark( ) {
                   print "Yip!\n";
           }
   }

?>

 </source>
   
  


Pass class instance as parameter

   <source lang="html4strict">

<?php

   class Employee {
       public $title;
       public $lastName;
       public $firstName;
       public $price;
       
       function __construct( $title, $firstName, $mainName, $price ) { 
           $this->title     = $title;
           $this->firstName = $firstName;
           $this->lastName  = $mainName;
           $this->price     = $price;
       }
       function getFullName() {
           return "{$this->firstName}" . " {$this->lastName}";
       }
   }
   

class EmployeeWriter {

   public function write( $shopProduct ) {
       $str  = "{$shopProduct->title}: ";   
       $str .= $shopProduct->getFullName();
       $str .= " ({$shopProduct->price})\n";
       print $str;
   }

} $product1 = new Employee( "Title", "A", "B", 5.9 ); $writer = new EmployeeWriter(); $writer->write( $product1 ); ?>


      </source>
   
  


The Method of a Child Class Overriding That of Its Parent (PHP 4 Syntax)

   <source lang="html4strict">

<?php

class Item {
  var $name;
  function Item( $name="item", $code=0) {
    $this->name = $name;
    $this->code = $code;
  }
 function getName() {
   return $this->name;
 }
}
class PriceItem extends Item {
  function getName() {
    return "(price).".$this->name;
  }
}
$item = new PriceItem( "widget", 5442 );
print $item->getName();
?>
 
 </source>