PHP/Class/Class Property — различия между версиями

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

Версия 10:37, 26 мая 2010

Accessing a Property from Within a Method

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



Adding Properties

 
<?
    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;
?>



Adding the $age variable to Cat

 
<?php
class Cat {
  var $age;
  //PHP 5 uses:
  //public $age;
}
?>



Assign default property value

<?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";
?>



Changing the Value of a Property from Within a Method

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



Class including a complete collection of get and set methods.

 
<?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("<p>%s is a %s and costs \$%.2f.</p>\n", $this->name, $this->breed, $this->price); 
    } 
} 
$magpie = new Dog("Malaysia", "magpie", 7.5); 
$magpie->display(); 
?>



Controlling Access to Class Members

 
<?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("<p>%s is a %s and costs \$%.2f.</p>\n",
            $this->name, $this->breed, $this->price);
  }
}
  $magpie = new Bird("Malaysia", "magpie", 7.5);
  $magpie->display();
?>



Define a setter and getter

 
<?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()."<br />";
   echo "drone2 employeeID: ".$drone2->getEmployeeID()."<br />";
   $drone1 = new Employee();
   $drone1->setEmployeeID("12345");
   $drone2 = clone $drone1;
   $drone2->setEmployeeID("67890");
   echo "employeeID: ".$drone1->getEmployeeID()."<br />";
   echo "employeeID: ".$drone2->getEmployeeID()."<br />";
?>



Define boolean Class properties

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