References, string offsets and overloaded objects

General discussion of development ideas and the approaches taken in the 3.x branch of phpBB. The current feature release of phpBB 3 is 3.3/Proteus.
Forum rules
Please do not post support questions regarding installing, updating, or upgrading phpBB 3.3.x. If you need support for phpBB 3.3.x please visit the 3.3.x Support Forum on phpbb.com.

If you have questions regarding writing extensions please post in Extension Writers Discussion to receive proper guidance from our staff and community.
Post Reply
Oleg
Posts: 1150
Joined: Tue Feb 23, 2010 2:38 am
Contact:

References, string offsets and overloaded objects

Post by Oleg »

Fatal error: Cannot create references to/from string offsets nor overloaded objects
I will give a cookie to whoever can explain:

1. What a "string offset" is; (is it a number?)

2. Why references to them cannot be created;

3. Why "overloaded objects" are a distinct class from "objects"; (I think I figured out what an "overloaded object" is)

4. Why references to "overloaded objects" cannot be created.

igorw
Registered User
Posts: 500
Joined: Thu Jan 04, 2007 11:47 pm

Re: References, string offsets and overloaded objects

Post by igorw »

nn- wrote:1. What a "string offset" is; (is it a number?)
$string = 'test';
echo $string[0]; // string offset 0 = t
echo $string[1]; // string offset 1 = e
echo $string[2]; // string offset 2 = s
echo $string[3]; // string offset 3 = t

It's basically the value of the string at a specific position.
nn- wrote:2. Why references to them cannot be created;
I guess the implementation would be messy and not really useful. Referencing the string and reading the requested offset is possible as a workaround.


Code producing the error?

ToonArmy
Registered User
Posts: 335
Joined: Fri Mar 26, 2004 7:31 pm
Location: Bristol, UK
Contact:

Re: References, string offsets and overloaded objects

Post by ToonArmy »

String offset:

Code: Select all

php -r '$bar = "foo"; $o = &$bar[1];'

Fatal error: Cannot create references to/from string offsets nor overloaded objects in Command line code on line 1
$string[$x] is equivalent to substr($string, $x, 1)

I wonder if the overloaded error is a throwback to PHP4's overload extension.
Chris SmithBlogXMOOhlohArea51WikiNo support via PM/IM
Image

User avatar
naderman
Consultant
Posts: 1727
Joined: Sun Jan 11, 2004 2:11 am
Location: Berlin, Germany
Contact:

Re: References, string offsets and overloaded objects

Post by naderman »

You can reference zvals. A particular character inside a string does not have its own zval. PHP does not know a character type. Referencing such a character would mean that each character needs to be treated as a variable with reference counting etc. which would mean a lot of memory overhead. And overloading in PHP refers to special __get and __set methods. So you cannot create a reference to $foo->bar when bar is really being accessed through __get/__set rather than being a real public attribute of the class which would have a zval to reference.

Oleg
Posts: 1150
Joined: Tue Feb 23, 2010 2:38 am
Contact:

Re: References, string offsets and overloaded objects

Post by Oleg »

I thought "overloaded object" meant "object with at least one property overloaded". Is it a misname for "return value of an overloaded property" then? (because the latter is clearly not necessarily an object)

User avatar
naderman
Consultant
Posts: 1727
Joined: Sun Jan 11, 2004 2:11 am
Location: Berlin, Germany
Contact:

Re: References, string offsets and overloaded objects

Post by naderman »

Yeah the error message is incorrect or at least misunderstandable.

Oleg
Posts: 1150
Joined: Tue Feb 23, 2010 2:38 am
Contact:

Re: References, string offsets and overloaded objects

Post by Oleg »

That's what I thought.
naderman wrote:You can reference zvals.
This is the truth, and a case of major abstraction leakage.

I knew intuitively that references were broken for quite some time, now I understand why they are. It all makes sense now: passing by reference, returning references, reference bugs labeled as features, etc.

Even perl gets them right:

Code: Select all

% perl -e '$a = \4; print $$a."\n";'
4
And this is the original code.

Code: Select all

<?php

date_default_timezone_set('UTC');

define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = 'php';
include($phpbb_root_path . 'common.' . $phpEx);

$user->date_format = '%b %m %Y | %b %m %Y';
$user->lang['datetime'] = '%b %m %Y';
$user->timezone = 'America/New_York';
print $user->format_date(time());
Here I am trying to test some timezone-related behavior, guessing what I need to do to bootstrap enough of phpbb so that format_date runs.

ToonArmy
Registered User
Posts: 335
Joined: Fri Mar 26, 2004 7:31 pm
Location: Bristol, UK
Contact:

Re: References, string offsets and overloaded objects

Post by ToonArmy »

You need to call the setup method on user. ;)
Chris SmithBlogXMOOhlohArea51WikiNo support via PM/IM
Image

User avatar
naderman
Consultant
Posts: 1727
Joined: Sun Jan 11, 2004 2:11 am
Location: Berlin, Germany
Contact:

Re: References, string offsets and overloaded objects

Post by naderman »

You wouldn't want to call setup in a test scenario but just set up as little as necessary to get date_format to work. Generally references in PHP should be avoided, see http://schlueters.de/blog/archives/125- ... ences.html

ToonArmy
Registered User
Posts: 335
Joined: Fri Mar 26, 2004 7:31 pm
Location: Bristol, UK
Contact:

Re: References, string offsets and overloaded objects

Post by ToonArmy »

naderman wrote:You wouldn't want to call setup in a test scenario but just set up as little as necessary to get date_format to work. Generally references in PHP should be avoided, see http://schlueters.de/blog/archives/125- ... ences.html
I wasn't aware this was unit testing related.
Chris SmithBlogXMOOhlohArea51WikiNo support via PM/IM
Image

Post Reply