add a note add a note User Contributed Notes
01-May-2006 09:06
Members can be added to instances on the fly.
Simply use
$apple= new fruit();
$pear=new fruit();
$apple->color='red';
$pear->smell='sweet';

and $apple only will contain a member (field) color, but $pear only will contain a field smell.

It is not clear however whether members an be added to the class at large on the fly.
26-Apr-2006 10:53
<?php
if (class_exists("cl_test")) return;

echo
"ppppp";

class
cl_test{
  function
cl_test(){
   echo
"quirky...";
  }
}
// class definition ends

?>

although the logic is clear php treats the above code differently.  PHP translates it to:

class cl_test{
  function cl_test(){
   echo "quirky...";
  }
} // class definition ends

if (class_exists("cl_test")) return;

echo "ppppp";

If you must have the previous logic to work you are forced to have the following code:

if (!class_exists("cl_test")) {

     echo "ppppp";

     class cl_test{
       function cl_test(){
           echo "quirky...";
       }
     } // class definition ends
}
epeterson5 at student dot gsu dot edu
08-Oct-2005 01:46
This isn't explicity noted anywhere, I don't think.
If you use a variable after the "->" part of a class attribute call, it will parse the variable as a string to assign/compare that class attribute:

<?php
class User {
   var
$test;
   var
$test2;
}
 
$user1 = new User();
$user2 = new User();
$user1->test = "user1's test";
$user1->test2 = "user1's test2";
$user2->test = "user2's test";
$user2->test2 = "user2's test2";

print_r($user1); print_r($user2);

foreach(
$user1 as $key => $value)
  
$user2->$key = $value;

print_r($user1); print_r($user2);
?>

Which outputs:

User Object: user1
   [test] => user1's test
   [test2] => user1's test2
User Object: user2
   [test] => user2's test
   [test2] => user2's test2
User Object: user1
   [test] => user1's test
   [test2] => user1's test2
User Object: user2
   [test] => user1's test
   [test2] => user1's test2

This is particularly useful when you want to assign a different instance of the same class to $this- I have a standard method that I call when I need to do that:

<?php

  
function assignToThis($new)  {
       foreach(
$new as $key => $value)
          
$this->$key=$value;
   }
?>
turgut85 at hotmail dot com
30-Jun-2005 09:04
<?PHP

// How to hold objects in an arrayList and retrieve it //
// Interface implementation //

class Data {
  
  
public $Name;
  
public $Age;
  
public $Address;
  
  
public function __construct($Name,$Age,$Address) {
      
$this->Name = $Name;
      
$this->Age = $Age;
      
$this->Address = $Address;
   }
  
  
public function __destruct() {
       echo
"Default Constructor...\n";
   }

}

$ArrayList = array();

$ArrayList[0] = new Data("John McDonald","65","Address");
$ArrayList[1] = new Data("Turgut Z. Yesilyurt","30","NJ, USA");
$ArrayList[2] = new Data("Maria ","25","NJ, USA");

print_r($ArrayList);

$obj1 = $ArrayList[1];
echo
"Name : ".$obj1->Name."\n";
echo
"Age  : ".$obj1->Age."\n";
echo
"Address : ".$obj1->Address."\n";

// Interface Usage //
interface ShowMe {
  
public function ShowName();
}

class
ShowData implements ShowMe {
  
public $obj;
public function __construct($Obj) {
  
$this->obj = $Obj;
  
$this->ShowName();
}

public function ShowName() {
   echo
"Age  : ".$this->obj->Age."\n";
}
}

echo
"\nDisplay Single Data =========================\n\n";
new
ShowData($ArrayList[1]);
echo
"\n=============================================\n";

// Turgut Z. YESILYURT, MS
// Software Developer //
// NJ, USA  //

?>

Array
(
   [0] => Data Object
       (
           [Name] => John McDonald
           [Age] => 65
           [Address] => Address
       )

   [1] => Data Object
       (
           [Name] => Turgut Z. Yesilyurt
           [Age] => 30
           [Address] => NJ, USA
       )

   [2] => Data Object
       (
           [Name] => Maria
           [Age] => 25
           [Address] => NJ, USA
       )

)
Name : Turgut Z. Yesilyurt
Age  : 30
Address : NJ, USA

Display Single Data =========================

Age  : 30

=============================================
Default Constructor...
Default Constructor...
Default Constructor...
zabmilenko at hotmail dot com
27-Jun-2005 03:27
Dynamic instantiation trick:

<?php

class CITY
{
  
private $population;

  
public function __construct($cityname)
   {
      
// Load some city-specific data
  
}

  
public function population($demographic = 'all')
   {
       return
$this->population[$demographic];
   }
}

class
COUNTRY
{
  
private $code = null;
  
private $cities = array();

  
public function __construct($code)
   {
      
$this->code = $code;
   }

  
public function city($cityname)
   {
       if (!
$this->cities[$cityname])
       {
          
$this->cities[$cityname] = new CITY($cityname);
       }

       return
$this->cities[$cityname];
   }
}

class
WORLD
{
  
private $countries = array();

  
public function country($code = 'us')
   {
       if (!
$this->countries[$code])
       {
          
$this->countries[$code] = new COUNTRY($code);
       }

       return
$this->countries[$code];
   }
}

$world = new WORLD;

// Load the country AND city object
echo $world->country('us')->city('seattle')->population('employed');

// Country US is already loaded, only need to load a new city object.
echo $world->country('us')->city('new york')->population();

?>

This example uses Countries and Cities wrapped around a World object.  You can use any schema you want, however.  Think:  Domain->Subdomain->Node or KINGDOM->PHYLUM->CLASS->ORDER->FAMILY->GENUS->SPECIES

What is happening here is that a private array is storing the class objects.  Only the class objects that are needed are loaded, when they are needed.  You can nest this many many times as needed.

You see that the array is never exposed.  Each function reference will check to see if a new class object is needed and create it if necessary.

This is literally as simple as it looks.  Hope it helps someone out.
Obeliks
11-Jun-2005 05:56
You can call parent::__construct(), even if the class yours inherits from uses the old constructor style "Classname()".

Example:

<?php

class A {
  function
A() {
   echo
'Constructor of A<br/>';
  }
}

class
B extends A {
  function
__construct() {
  
parent::__construct();
   echo
'Constructor of B<br/>';
  }
}

class
C extends A {
  function
__construct() {
  
parent::A();
   echo
'Constructor of C<br/>';
  }
}

$b = new B();
echo
'<br/>';
$c = new C();

/* will output:
Constructor of A
Constructor of B

Constructor of A
Constructor of C
*/

?>

So you see you can also call parent::Classname() if your superclass uses this format. Keep in mind that it doesn't work the other way round though:

<?php

class A {
  function
__construct() {
   echo
'Constructor of A<br/>';
  }
}

class
B extends A {
  function
__construct() {
  
parent::A();
   echo
'Constructor of B<br/>';
  }
}

$b = new B();

/* will output:
Fatal error: Call to undefined method A::a() in __FILE__ on line __LINE__
*/

?>

So it's always the best choice to use the __construct style! (at least if you're running PHP5)
bartlewis at gmail dot com
08-Jun-2005 09:29
In regards to gaehngaehn at hotmail dot com...

More simply, to create an object from a class using a variable, just try the following.

$a = new $class();

Works in php4 and php5.
Sb.
28-May-2005 12:50
We can't create easily anonymous objects like in JavaScript.
JS example :

   var o = {
       aProperty : "value",
       anotherProperty : [ "element 1", "element 2" ] } ;
   alert(o.anotherProperty[1]) ; // "element 2"

So I have created a class Object :

   class Object {
       function __construct( ) {
           $n = func_num_args( ) ;
           for ( $i = 0 ; $i < $n ; $i += 2 ) {
               $this->{func_get_arg($i)} = func_get_arg($i + 1) ;
           }
       }
   }

   $o = new Object(
       'aProperty', 'value',
       'anotherProperty', array('element 1', 'element 2')) ;
   echo $o->anotherProperty[1] ; // "element 2"

You must feel free to make it better :)
AJG
16-May-2005 11:13
[Note from et@php.net:
Your way to create a new, empty, generic object is:
<?php $obj = new stdClass(); ?>
]

I never found a way to create a new, empty, generic object so I coded something up myself. In addition, let me show you some imperfect examples.

<?
  
// Incorrect:   
  
$obj1 = (object) '';
  
$obj2 = (object) 0;
  
$obj3 = (object) 1;
  
$obj4 = (object) true;
  
$obj5 = (object) false;

  
// Syntax error:
  
$obj6 = (object);

  
// Correct:
  
$obj7 = (object) NULL;
?>

The first five ones will work, but will create a 'scalar' field in the object. This is undesirable at best, and can be a real nuisance when doing object automation. For that reason, I now use the following function, which produces a 'clean' object. It's also better semantically, in my opinion.

<?
  
function object() { return ((object) NULL); }

  
// Usage:
  
$object = object();

  
// Just like:
  
$array = array();
?>

Please post if you see any problems arising due to this method; thanks. Otherwise enjoy.
--AJG.
29-Mar-2005 07:21
There are 3 simple, and utterly annoying problems with your classes (not because of how you want them to work, but because how the Zend II engine handles them):

1) You cannot type hint string or int/integer in a method signature. This is incredibly annoying that the Zend II engine doesn't support it, but there are workarounds.
2) Supplying null in a method signature for a default value means that you will not accept any null value for that parameter. (therefore the method doesn't need to check if the parameters are null anyway).
3) Sadly, overriding methods is only possible with the Zend II engine via Inheritance or Polymorphism, ( and __construct() can only be defined within a class). If you want to override a method in the same class, my suggestion is to provide the method signature with a $flag = null variable, which you call a SWITCH on to pick what the data should do.

==============================================
Other than the afformentioned, the Zend II engine works very similarly to Java, which has made PHP much more versatile and robust in version 5. Thank you again Zend!
spam at afoyi dot com
21-Mar-2005 09:18
You can call a function defined in an inherited class from the parent class. This works in both PHP 4.3.6 and 5.0.0:

<?php

class p {
  
   function
p() {
       print
"Parent's constructor\n";
   }
  
   function
p_test() {
       print
"p_test()\n";
      
$this->c_test();
   }
}

class
c extends p {
  
   function
c() {
       print
"Child's constructor\n";
      
parent::p();
   }
  
   function
c_test() {
       print
"c_test()\n";
   }
}

$obj = new c;
$obj->p_test();

?>

Outputs:

Child's constructor
Parent's constructor
p_test()
c_test()
farzan at ifarzan dot com
06-Oct-2004 07:04
PHP 5 is very very flexible in accessing member variables and member functions. These access methods maybe look unusual and unnecessary at first glance; but they are very useful sometimes; specially when you work with SimpleXML classes and objects. I have posted a similar comment in SimpleXML function reference section, but this one is more comprehensive.

I use the following class as reference for all examples:
<?
class Foo {
  
public $aMemberVar = 'aMemberVar Member Variable';
  
public $aFuncName = 'aMemberFunc';
  
  
   function
aMemberFunc() {
       print
'Inside `aMemberFunc()`';
   }
}

$foo = new Foo;
?>

You can access member variables in an object using another variable as name:
<?
$element
= 'aMemberVar';
print
$foo->$element; // prints "aMemberVar Member Variable"
?>

or use functions:
<?
function getVarName()
{ return
'aMemberVar'; }

print
$foo->{getVarName()}; // prints "aMemberVar Member Variable"
?>

Important Note: You must surround function name with { and } or PHP would think you are calling a member function of object "foo".

you can use a constant or literal as well:
<?
define
(MY_CONSTANT, 'aMemberVar');
print
$foo->{MY_CONSTANT}; // Prints "aMemberVar Member Variable"
print $foo->{'aMemberVar'}; // Prints "aMemberVar Member Variable"
?>

You can use members of other objects as well:
<?
print $foo->{$otherObj->var};
print
$foo->{$otherObj->func()};
?>

You can use mathods above to access member functions as well:
<?
print $foo->{'aMemberFunc'}(); // Prints "Inside `aMemberFunc()`"
print $foo->{$foo->aFuncName}(); // Prints "Inside `aMemberFunc()`"
?>