PHP/Class/const

Материал из Web эксперт
Версия от 10:00, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Constant definition is considered a static member of the class

   <source lang="html4strict">

<?php class myclass {

 const MYCONST = 123;
 private static $value = 567;

} echo "myclass::MYCONST = " . myclass::MYCONST . "\n"; echo "myclass::$value = " . myclass::$value . "\n"; ?>

 </source>
   
  


Counting the elements in an array

   <source lang="html4strict">

<?php $shapes = array("S" => "Cylinder",

               "N" => "Rectangle",
               "A" => "Sphere",
               "O" => "Sphere",
               "P" => "Rectangle");

$numElements = count($shapes); print "The array has $numElements elements.
"; ?>

 </source>
   
  


Finding the size of an array

   <source lang="html4strict">

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

 </source>
   
  


Using Class Constants

   <source lang="html4strict">

<?php class Employee {

   const CATEGORY_WORKER = 0; 
   const CATEGORY_SUPERVISOR = 1; 
   const CATEGORY_MANAGER = 2; 
   

} ?>

 </source>
   
  


Using Class Constants in PHP5

   <source lang="html4strict">

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

?>

 </source>