From:             
Operating system: *
PHP version:      Irrelevant
Package:          Class/Object related
Bug Type:         Feature/Change Request
Bug description:New magic method for procedural calls on object.

Description:
------------
<?php

$a = Array();

$a['a'] = 'text';

$a['b'] = 15;

$a['c'] = 155.12;

?>



Classic style:

<?php

ksort( $a );

is_array( $a );

?>



Object style:

<?php

$oa = new ObjArray($a); // extends ArrayIterator or ArrayObject

$oa->ksort();

$oa->is_array(); //implemented in ObjArray

?>



But what if :

<?php

$oa = new ObjArray($a); // extends ArrayIterator or ArrayObject

ksort( $oa );

is_array( $oa );

?>



(SplTypes project is dead and I need object types.)

Now it is not possible to control, what functions are executed on object.





If a language function is used on a object, php would 

Variant 1) try to call object function of the same name,

Variant 2) call magic method __callOnObject if exists 



Both variants can be implemented together.





Possible problem in variant 1)

Programmer would need to take care, to really implement exact same
structure of parameters, with all optional parameters, elze it would throw
errors on different arguments.

There could by a variant 1a) where, called function doesn't list all
arguments, but passes an array of arguments. But that would make object
calls too different from procedural.







Test script:
---------------
Variant 1)



class ObjArray extends ArrayIterator{

  public function is_array( $var ){

    return is_array( $this->storage );

  }

  public function is_object( $var ){

    return is_object( $this->storage );

  }

}



$obj = new ObjArray( Array( 1, 2 ) );

var_dump( is_array( $obj ) );

var_dump( is_object( $obj ) );







Variant 2)



class ObjArray extends ArrayIterator{

  public function __callOnObject( $calledFunction, $params ){

    switch $calledFunction {

      case "is_array":

        return $this->is_array( $params[0] );

        break;

      case "is_object":

        return $this->is_object( $params[0] );

        break;

      default:

        return $calledFunction( insert_params_irrelevant_how( $params ) );

    }

  }

  public function is_array( $var ){

    return is_array( $this->storage );

  }

  public function is_object( $var ){

    return is_object( $this->storage );

  }

}



$obj = new ObjArray( Array( 1, 2 ) );

var_dump( is_array( $obj ) );

var_dump( is_object( $obj ) );







Expected result:
----------------
Variant 1)



//$obj = new ObjArray( Array( 1, 2 ) );

//var_dump( is_array( $obj ) );

//should translate to var_dump( $obj->is_array( $obj ) );

bool(true)

//var_dump( is_object( $obj ) );

//should translate to var_dump( $obj->is_object( $obj ) );

bool(false)





Variant 2)

//$obj = new ObjArray( Array( 1, 2 ) );

//var_dump( is_array( $obj ) );

//should translate to var_dump( $obj->__callOnObject( "is_array", Array(
$obj ) ) );

//swith/case finds function is_array and calls $obj->is_array( $obj );

bool(true)

//var_dump( is_object( $obj ) );

//should translate to var_dump( $obj->__callOnObject( "is_object", Array(
$obj ) ) );

//swith/case finds function is_object and calls $obj->is_object( $obj );

bool(false)









Actual result:
--------------
//$obj = new ObjArray( Array( 1, 2 ) );

//var_dump( is_array( $obj ) );

bool(false)

//var_dump( is_object( $obj ) );

bool(true)











-- 
Edit bug report at http://bugs.php.net/bug.php?id=52626&edit=1
-- 
Try a snapshot (PHP 5.2):            
http://bugs.php.net/fix.php?id=52626&r=trysnapshot52
Try a snapshot (PHP 5.3):            
http://bugs.php.net/fix.php?id=52626&r=trysnapshot53
Try a snapshot (trunk):              
http://bugs.php.net/fix.php?id=52626&r=trysnapshottrunk
Fixed in SVN:                        
http://bugs.php.net/fix.php?id=52626&r=fixed
Fixed in SVN and need be documented: 
http://bugs.php.net/fix.php?id=52626&r=needdocs
Fixed in release:                    
http://bugs.php.net/fix.php?id=52626&r=alreadyfixed
Need backtrace:                      
http://bugs.php.net/fix.php?id=52626&r=needtrace
Need Reproduce Script:               
http://bugs.php.net/fix.php?id=52626&r=needscript
Try newer version:                   
http://bugs.php.net/fix.php?id=52626&r=oldversion
Not developer issue:                 
http://bugs.php.net/fix.php?id=52626&r=support
Expected behavior:                   
http://bugs.php.net/fix.php?id=52626&r=notwrong
Not enough info:                     
http://bugs.php.net/fix.php?id=52626&r=notenoughinfo
Submitted twice:                     
http://bugs.php.net/fix.php?id=52626&r=submittedtwice
register_globals:                    
http://bugs.php.net/fix.php?id=52626&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=52626&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=52626&r=dst
IIS Stability:                       
http://bugs.php.net/fix.php?id=52626&r=isapi
Install GNU Sed:                     
http://bugs.php.net/fix.php?id=52626&r=gnused
Floating point limitations:          
http://bugs.php.net/fix.php?id=52626&r=float
No Zend Extensions:                  
http://bugs.php.net/fix.php?id=52626&r=nozend
MySQL Configuration Error:           
http://bugs.php.net/fix.php?id=52626&r=mysqlcfg

Reply via email to