PHP/Class/Class Property

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

Accessing a Property from Within a Method

   <source lang="html4strict">

<?php class Item {

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

?>

 </source>
   
  


Adding Properties

   <source lang="html4strict">

<?

   class Dog {
           public $Name;
           public function bark( ) {
                   print "Woof!\n";
           }
   }
   $poppy = new Poodle;
   $penny = new Poodle;
   $poppy->Name = "Poppy";
   $penny->Name = "Penny";
   print $poppy->Name;

?>

 </source>
   
  


Adding the $age variable to Cat

   <source lang="html4strict">

<?php class Cat {

 var $age;
 //PHP 5 uses:
 //public $age;

} ?>

 </source>
   
  


Assign default property value

   <source lang="html4strict">

<?php

   class Employee {
       public $title       = "product";
       public $mainName    = "main";
       public $firstName   = "first";
       public $price       = 0;
       
       function getFullName() {
           return "{$this->firstName}".
                  " {$this->mainName}";
       }
   }

$product1 = new Employee(); $product1->title = "title"; $product1->mainName = "A"; $product1->firstName = "B"; $product1->price = 5.99; print "author: ".$product1->getFullName()."\n"; ?>


      </source>
   
  


Changing the Value of a Property from Within a Method

   <source lang="html4strict">

<?php

class Item {
  var $name = "item";
  function setName( $n ) {
    $this->name = $n;
  }
  function getName() {
    return $this->name;
  }
}
$item = new Item();
$item->setName("widget 5442");
print $item->getName ();

?>

 </source>
   
  


Class including a complete collection of get and set methods.

   <source lang="html4strict">

<?php class Dog{

   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 Dog("Malaysia", "magpie", 7.5); $magpie->display(); ?>

 </source>
   
  


Controlling Access to Class Members

   <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();

?>

 </source>
   
  


Define a setter and getter

   <source lang="html4strict">

<?php

  class Employee{
     private $employeeid;
     function setEmployeeID($employeeid) {
        $this->employeeid = $employeeid;
     }
     function getEmployeeID() {
        return $this->employeeid;
     }
  }
  $drone1 = new Employee();
  $drone1->setEmployeeID("12345");
  $drone2 = clone $drone1;
  $drone2->setEmployeeID("67890");
  echo "drone1 employeeID: ".$drone1->getEmployeeID()."
"; echo "drone2 employeeID: ".$drone2->getEmployeeID()."
"; $drone1 = new Employee(); $drone1->setEmployeeID("12345"); $drone2 = clone $drone1; $drone2->setEmployeeID("67890"); echo "employeeID: ".$drone1->getEmployeeID()."
"; echo "employeeID: ".$drone2->getEmployeeID()."
";

?>

 </source>
   
  


Define boolean Class properties

   <source lang="html4strict">

<?php

    class WebClient
    {
         var $frames;
         var $tables;
         var $layers;
    }
    
    
    $my_browser = new WebClient;     
    
    $my_browser->frames = true;
    $my_browser->tables = true;
    $my_browser->layers = true;

print("Browser Has Frames: " . $my_browser->frames); ?>

      </source>