PHP/Class/Objects

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

Comparing Objects with == and ===

   <source lang="html4strict">

<?php

   class Employee { }
   $Bob = new Employee( );
   $Joe = clone $Bob;
   print (int)($Bob == $Joe) . "\n";
   print (int)($Joe === $Joe) . "\n";
   class Employee {
           public function __construct( ) {
                   $this->myself = $this;
           }
   }
   $Bob = new Employee( );
   $Joe = clone $Bob;
   print (int)($Bob == $Joe) . "\n";
   print (int)($Bob === $Joe) . "\n";
   

?>

 </source>
   
  


Create a new class and create an instance then use its property and method

   <source lang="html4strict">

<?php class Dog {

 var $name;
 function bark()
 {
   print "Woof!";
 }

} $pooch = new Dog; $pooch -> name = "new name"; print $pooch -> name . " says "; $pooch -> bark(); ?>

 </source>
   
  


Creating a new object and assigning it to a variable

   <source lang="html4strict">

<?php Class Cat {

 // Constructor
 function __constructor(  ) {
 }
 // The cat meows
 function meow(  ) {
   echo "Meow...";
 }
 // The cat eats
 function eat(  ) {
   echo "*eats*";
 }
 // The cat purrs
 function purr(  ) {
   echo "*Purr...*";
 }

} $myCat=new Cat; ?>

 </source>
   
  


Object Initialization

   <source lang="html4strict">

<?php class foo {

   function do_foo() {
       echo "Doing foo.";
   }

} $bar = new foo; $bar->do_foo(); ?>

 </source>
   
  


Object Overloading

   <source lang="html4strict">

<?php class Data {

   private $data = array();
   public function __set($name, $value) {
       $this->data[$name] = $value;
   }
   public function __get($name) {
       if (isset($this->data[$name])) { return $this->data[$name]; }
   }
   public function __isset($name) {
       return isset($this->data[$name]);
   }
   public function __unset($name) {
       unset($this->data[$name]);
   }

} $data = new Data(); $data->name = "F";

echo "

The data value of "name" is {$data->name}

";

unset($data->name);

echo "

The value is ", isset($data->name) ? "" : "not ", "set.

";

?>

 </source>
   
  


Object Properties

   <source lang="html4strict">

<? class Item {

 var $name = "item";

} $obj1 = new Item(); $obj2 = new Item(); $obj1->name = "widget 5442"; print "$obj1->name
"; print "$obj2->name
"; ?>

 </source>
   
  


Objects Within Objects

   <source lang="html4strict">

<?

   class DogTag {
           public $Words;
   }
   class Dog {
           public $Name;
           public $DogTag;
           public function bark( ) {
                   print "Woof!\n";
           }
   }
   $poppy = new Poodle;
   $poppy->Name = "Poppy";
   $poppy->DogTag = new DogTag;
   $poppy->DogTag->Words = "call 555-1234";

?>

 </source>
   
  


Object Type Information

   <source lang="html4strict">

<?

   class Dog {
           public $Name;
           private function getName( ) {
                   return $this->Name;
           }
   }
   class Poodle extends Dog {
           public function bark( ) {
                   print ""Woof", says " . $this->getName( );
           }
   }
   $poppy = new Poodle;
   $poppy->Name = "Poppy";
   $poppy->bark( );
   if ($poppy instanceof poodle) { }
   if ($poppy instanceof dog) { }

?>

 </source>