|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2010-08-17 18:00 UTC] jay at jay dot cz
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)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 19:00:01 2025 UTC |
This problem would be fixed if PHP allowed casting to other scalar types then strings and casting to arrays. How it would work internally: ksort($obj); // => ksort((array) $obj); // => ksort($obj->__cast('array')); Bug #52807 deals with __toArray / cast to array.