search for in the  
<Comparing objectsThe Basics>
Last updated: Thu, 19 May 2005

Chapter 19. Classes and Objects (PHP 5)

Introduction

In PHP 5 there is a new Object Model. PHP's handling of objects has been completely rewritten, allowing for better performance and more features.



User Contributed Notes
Classes and Objects (PHP 5)
AJG
16-May-2005 11:13
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.
28-Mar-2005 06: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!
hans at lintoo dot dk
23-Mar-2005 12:01
Multiple methods with same name but different signature does not compile.

trying to do:

<?php
public function __construct(string $myString) {
    
// do something with string
}
public function
__construct(int $myInt) {
    
// do something with int
}
?>

does not work, perhaps because you cant do type hinting with string and int, but then you should be able to create them as objects, which would give:

<?php
public function __construct(String $myString) {
    
// do something with string
}
public function
__construct(Integer $myInt) {
    
// do something with int
}
?>

but that don't work either so you need to do:

<?php
public function __construct($myString=null,$myInt=null) {
     if (!
is_null($myInt)) {
          
// do something with int
    
} else if (!is_null($myString)) {
        
// do something with string
    
} else {
         throw new
Exception("Error: __construct called without string *OR* int";
     }
}
?>

Hans Duedal // http://www.lintoo.dk/
spam at afoyi dot com
20-Mar-2005 08: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
05-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()`"
?>

<Comparing objectsThe Basics>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 18:35:34 2005 EDT