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

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

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

A Class with Public Properties

 
<?php
 class item {
   var $name;
   var $code;
   var $productString;
   function Item( $name="item", $code=0 ) {
     $this->name = $name;
     $this->code = $code;
    $this->setName( $name );
 }
 function getProductString () {
   return $this->productString;
  }
  function setName( $n ) {
    $this->name = $n;
    $this->productString = $this->name." ".$this->code;
  }
  function getName () {
    return $this->name;
  }
 }
 $item = new Item ("A", 5);
 print $item->getProductString ();
 ?>



Public fields

<?php
   class Staff
   {
      public $name;
   }
   $employee = new Staff();
   $employee->name = "Joe";
   $name = $employee->name;
   echo "www.wbex.ru: $name";
?>



public method

<?php
   class Staff
   {
      private $name;
      public function setName($name) {
         $this->name = $name;
     }
   }
   $staff = new Staff;
   $staff->setName("Mary");
?>



Use public properties directly

<?php
    class Employee {
        public $title       = "name";
        public $mainName    = "main name";
        public $firstName   = "first name";
    }
$product1 = new Employee();
$product1->title = "title";
$product1->mainName  = "A";
$product1->firstName = "B";
print "author: {$product1->firstName} " . "{$product1->mainName}\n";
?>



Using Class Visibility Operators

 
<?php
class cd {
    public $artist;
    public $title;
    protected $tracks;
    private $disk_id;
}
$mydisk = new cd();
$mydisk->artist = "Singers";
$mydisk->tracks = array("Bus", "Little");
?>