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

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

Текущая версия на 10:00, 26 мая 2010

A Class with Public Properties

   <source lang="html4strict">

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


Public fields

   <source lang="html4strict">

<?php

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

?>

      </source>
   
  


public method

   <source lang="html4strict">

<?php

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

?>

      </source>
   
  


Use public properties directly

   <source lang="html4strict">

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

      </source>
   
  


Using Class Visibility Operators

   <source lang="html4strict">

<?php class cd {

   public $artist;
   public $title;
   protected $tracks;
   private $disk_id;

} $mydisk = new cd(); $mydisk->artist = "Singers"; $mydisk->tracks = array("Bus", "Little"); ?>

 </source>