Skip to content

Declare Transliterator::$id as readonly to unlock subclassing it #9167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions ext/intl/tests/transliterator_child.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
Subclass Transliterator
--EXTENSIONS--
intl
--FILE--
<?php
class ChildTransliterator extends Transliterator
{
public readonly string $id;

public static function new()
{
$tr = (new \ReflectionClass(ChildTransliterator::class))->newInstanceWithoutConstructor();
$tr->id = 'abc';

return $tr;
}
}


$tr = ChildTransliterator::new();

var_dump($tr->id);
?>
--EXPECT--
string(3) "abc"
2 changes: 1 addition & 1 deletion ext/intl/transliterator/transliterator.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/** @not-serializable */
class Transliterator
{
public string $id;
public readonly string $id;

final private function __construct() {}

Expand Down
4 changes: 2 additions & 2 deletions ext/intl/transliterator/transliterator_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 3 additions & 64 deletions ext/intl/transliterator/transliterator_class.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,22 +144,11 @@ static zend_object *Transliterator_clone_obj( zend_object *object )

if( to_orig->utrans != NULL )
{
zval tempz; /* dummy zval to pass to transliterator_object_construct */

/* guaranteed to return NULL if it fails */
UTransliterator *utrans = utrans_clone( to_orig->utrans, TRANSLITERATOR_ERROR_CODE_P( to_orig ) );

if( U_FAILURE( TRANSLITERATOR_ERROR_CODE( to_orig ) ) )
goto err;

ZVAL_OBJ(&tempz, ret_val);
transliterator_object_construct( &tempz, utrans,
TRANSLITERATOR_ERROR_CODE_P( to_orig ) );

if( U_FAILURE( TRANSLITERATOR_ERROR_CODE( to_orig ) ) )
{
if( U_FAILURE( TRANSLITERATOR_ERROR_CODE( to_orig ) ) ) {
zend_string *err_msg;
err:

if( utrans != NULL )
transliterator_object_destroy( to_new );
Expand All @@ -173,7 +162,8 @@ static zend_object *Transliterator_clone_obj( zend_object *object )
err_msg = intl_error_get_message( TRANSLITERATOR_ERROR_P( to_orig ) );
zend_throw_error( NULL, "%s", ZSTR_VAL(err_msg) );
zend_string_free( err_msg ); /* if it's changed into a warning */
/* do not destroy tempz; we need to return something */
} else {
to_new->utrans = utrans;
}
}
else
Expand All @@ -186,54 +176,6 @@ static zend_object *Transliterator_clone_obj( zend_object *object )
}
/* }}} */

/* {{{ get_property_ptr_ptr handler */
static zval *Transliterator_get_property_ptr_ptr( zend_object *object, zend_string *name, int type, void **cache_slot )
{
if (zend_string_equals_literal(name, "id")) {
return NULL; /* fallback to read_property */
}
return zend_std_get_property_ptr_ptr( object, name, type, cache_slot );
}
/* }}} */

/* {{{ read_property handler */
static zval *Transliterator_read_property( zend_object *object, zend_string *name, int type, void **cache_slot, zval *rv )
{
zval *retval;

if ((type != BP_VAR_R && type != BP_VAR_IS) && zend_string_equals_literal(name, "id")) {
zend_throw_error(NULL, "Transliterator::$id is read-only");
retval = &EG( uninitialized_zval );
} else {
retval = zend_std_read_property( object, name, type, cache_slot, rv );
}

return retval;
}

/* }}} */

/* {{{ write_property handler */
static zval *Transliterator_write_property( zend_object *object, zend_string *name, zval *value,
void **cache_slot )
{
zend_class_entry *scope;

if (EG(fake_scope)) {
scope = EG(fake_scope);
} else {
scope = zend_get_executed_scope();
}
if ((scope != Transliterator_ce_ptr) && zend_string_equals_literal(name, "id")) {
zend_throw_error(NULL, "Transliterator::$id is read-only");
} else {
value = zend_std_write_property( object, name, value, cache_slot );
}

return value;
}
/* }}} */

/* {{{ transliterator_register_Transliterator_class
* Initialize 'Transliterator' class
*/
Expand All @@ -246,9 +188,6 @@ void transliterator_register_Transliterator_class( void )
Transliterator_handlers.offset = XtOffsetOf(Transliterator_object, zo);
Transliterator_handlers.free_obj = Transliterator_objects_free;
Transliterator_handlers.clone_obj = Transliterator_clone_obj;
Transliterator_handlers.get_property_ptr_ptr = Transliterator_get_property_ptr_ptr;
Transliterator_handlers.read_property = Transliterator_read_property;
Transliterator_handlers.write_property = Transliterator_write_property;

/* constants are declared in transliterator_register_constants, called from MINIT */

Expand Down