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

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

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

Constant definition is considered a static member of the class

 
<?php
class myclass {
  const MYCONST = 123;
  private static $value = 567;
}
echo "myclass::MYCONST = " . myclass::MYCONST . "\n";
echo "myclass::$value = " . myclass::$value . "\n";
?>



Counting the elements in an array

 
<?php
$shapes = array("S" => "Cylinder",
                "N" => "Rectangle",
                "A" => "Sphere",
                "O" => "Sphere",
                "P" => "Rectangle");
$numElements = count($shapes);
print "The array has $numElements elements.<br />";
?>



Finding the size of an array

 
<?
$dinner = array("A","B","C");
$dishes = count($dinner);
print "There are $dishes things for dinner.";
?>



Using Class Constants

 
<?php 
class Employee { 
    const CATEGORY_WORKER = 0; 
    const CATEGORY_SUPERVISOR = 1; 
    const CATEGORY_MANAGER = 2; 
    
}
?>



Using Class Constants in PHP5

 
<?php
     class MyExample {
          private $myvar;
          public $readme;
          const MY_CONSTANT = 10;
          public function showConstant() {
               echo "The value is: ".MY_CONSTANT;
          }
     }
     $inst = new MyExample;
     $inst->showConstant();
     echo "The value: ".ConstExample::MY_CONSTANT;
?>