Hi All,
I'm reasonably proficient with C#, and am starting with php5. I was happy
to learn of the __get and __set methods, but am encountering a problem using
them to set an object property of type "array".
Below is a simple example of the problem I'm having with a sample object
"Article", which has an array property "authors". As you can see, I can't
even use standard php array syntax to set a single author, even though the
same syntax outside of the object works perfectly.
Am I doing something wrong here, or is this a php5 bug?
TIA,
JON
---------------------------------------
//test with normal array
$array = array();
$array[] = "Mr Jones";
print $array[0] . "<br>";
// expected result: "Mr Jones"
// actual result: "Mr Jones"
//test with array property
$article = new Article();
$article->title = "my title";
$article->authors[] = "Mr Smith";
print $article->authors[0] . "<br>";
// expected result: "Mr Smith"
// actual result: "Notice: Undefined offset: 0"
//diagnosis: authors array is *not* being filled.
// var_dump($artic le->authors);
// gives
// result: array(0) { }
class Article
{
protected $properties = array(
'title' => "",
'authors' => array()
);
public function __construct()
{
}
public function __get($property )
{
if( array_key_exist s($property, $this->properties) )
{
return $this->properties[$property];
}
else
{
throw new Exception();
}
}
public function __set($property , $value)
{
if( array_key_exist s($property, $this->properties) )
{
$this->properties[$property] = $value;
}
else
{
throw new Exception();
}
}
} 5 5537
Not really a bug..more a difference in the way __get and __set are
handled. Remember that when you use these, something like:
$x = $obj->y
does something slightly different than what you expect. Consider the
following:
function makeArray() {
$r = array(1, 2, 3, 4);
return $r;
}
Could you then write something like this:
echo makeArray()[2];
No, because makeArray is a function, not a reference or variable. The
same thing happens when you use __get() and __set(). Although it looks
like regular property accessing and setting, it really isn't.
This is the same reason you can't use overloaded __get() and __set with
empty(), unset(), etc. (well, now you can overload those, too).
The simplest solution to your problem is to pull out the array before
you try and access or set it's element:
$article = new Article();
$article->title = "my title";
$authors = array("Mr. Smith");
$article->authors = $authors;
print $authors[0] . "<br>";
ZeldorBlat said the following on 07/11/2005 14:34: Not really a bug..more a difference in the way __get and __set are handled. Remember that when you use these, something like:
$x = $obj->y
does something slightly different than what you expect. Consider the following:
function makeArray() { $r = array(1, 2, 3, 4); return $r; }
Could you then write something like this:
echo makeArray()[2];
No, because makeArray is a function, not a reference or variable.
Actually, this is a limitation of PHP. In C, C++, C# and Java, the
equivalent of the above code works fine.
--
Oli
On 7 Nov 2005 06:34:47 -0800, "ZeldorBlat " <ze********@gma il.com>
wrote: No, because makeArray is a function, not a reference or variable. The same thing happens when you use __get() and __set(). Although it looks like regular property accessing and setting, it really isn't.
Well you're right that you can't do someFunction()[0] but array access
syntax does work for __get and _set. Try this in PHP5 and you'll get
the desired result:
class Test
{
public function __get($name) {
return Array('hello', 'world');
}
public function blah()
{
return Array('hello', 'world');
}
}
$test = new Test();
echo $test->whatever[0];
// displays 'hello'
The original posters problem is entirely different.
On Mon, 7 Nov 2005 10:48:57 -0000, "Jon Maz"
<jo****@surfeu. no.spam.de> wrote: Hi All,
I'm reasonably proficient with C#, and am starting with php5. I was happy to learn of the __get and __set methods, but am encountering a problem using them to set an object property of type "array".
Below is a simple example of the problem I'm having with a sample object "Article", which has an array property "authors". As you can see, I can't even use standard php array syntax to set a single author, even though the same syntax outside of the object works perfectly.
Am I doing something wrong here, or is this a php5 bug?
The problem is that arrays are values in PHP. So when you return an
array from a function (or assign it to variable) you're really
returing/assigning a copy of the array.
//test with array property $article = new Article(); $article->title = "my title"; $article->authors[] = "Mr Smith";
This line adds "Mr Smith" to a copy of the authors array (as returned
by the __get function) and not the original array.
Your best bet is to use an ArrayObject rather than an array. It works
just like an array but, like all objects in PHP5, it's passed by
reference and should work as you expect.
Later,
Hi All,
Thanks for the responses, they've helped me understand what's going on.
-------------
I think Oli hit the nail on the head when he wrote: Could you then write something like this: echo makeArray()[2]; No, because makeArray is a function, not a reference or variable.
Actually, this is a limitation of PHP. In C, C++, C# and Java, the equivalent of the above code works fine.
I was indeed expecting PHP to work just like C#. Does anybody know if
there are plans to change this PHP behavior? Is this regarded as a bug
or a feature? (My vote is for bug).
-------------
In the end I have decided to solve the problem by replacing my properties
of type array() with properties of type CollectionBase, as found
here: http://coding.mu/archives/2003/08/09...lection-class/. I think
this is the solution Wayne was suggesting when he proposed using an
object instead of an array.
I decided to switch to this Collection object before Wayne gave the
ArrayObject tip. I'd never heard of ArrayObject, but have found some
useful info on it. Urls here in case they help anyone else: http://www.sitepoint.com/article/php...dard-library/2 http://www.ramikayyali.com/archives/...2/25/iterators http://www.php.net/~helly/php/ext/sp...rayObject.html http://www.devshed.com/index2.php?op...ge=0&hide_js=1
-------------
Finally, in my digging around on the net I found the following. I don't
100% follow the argument here, but it sounds like this guy is discussing
the same issue I came across (true?). http://uk.php.net/manual/en/language...verloading.php A few things I've found about __get()...
First off, if you use $obj-> getOne-> getAnother, both intended to be resolved by __get, the __get() function only sees the first one at first. You can't access the second one. You can, however, return the pointer to an object that can handle the second one. In short, you can have the same class handle both by returning a new object with the data changed however you see fit.
Secondly, when using arrays like: $obj-> getArray["one"], only the array name is passed on to __get. However, when you return the array, PHP treats it just as it should. THat is, you'd have to make an array with the index of "one" in __get in order to see any results. You can also have other indexes in there as well.
Also, for those of you like me, I've already tried to use func_get_args to see if you can get more than just that one.
If you're like me and were hoping you could pass some sort of argument onto __get in order to help gather the correct data, you're out of look. I do recommend using __call though. You could easily rig __call up to react to certain things, like: $account-> properties( "type" ); , which is my example. I'm using DOM for data storage (for now), and I'm trying to make an interface that'll let me easily switch to something else - MySQL, flat file, anything. This would work great though!
-------------
Thanks once again for all the help,
JON This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Scott Auge |
last post by:
I am looking for comments on something that lets me abstract database
updates in an object.
Lemme explain what I am thinking:
Lets say I have an object Person with...
SetFirstName()
SetLastName()
|
by: JGH |
last post by:
How can I check if a key is defined in an associative array?
var users = new array();
users = "Joe Blow";
users = "John Doe";
users = "Jane Doe";
function isUser (userID)
{
if (?????)
|
by: Denis Perelyubskiy |
last post by:
Hello,
I need to make an array of elements accross forms.
My javascript skills, as evident from this question,
are rather rudimentary.
I tried to make an associative array and index it
with the object references. However, I just realized
that indices may only be referenced by strings.
|
by: VK |
last post by:
Or why I just did myArray = "Computers" but myArray.length is
showing 0. What a hey?
There is a new trend to treat arrays and hashes as they were some
variations of the same thing. But they are not at all.
If you are doing *array", then you have to use only integer values for
array index, as it was since ALGOL.
|
by: VK |
last post by:
Whatever you wanted to know about it but always were affraid to ask.
<http://www.geocities.com/schools_ring/ArrayAndHash.html>
| |
by: Robert Mark Bram |
last post by:
Hi All!
How do you get the length of an associative array?
var my_cars= new Array()
my_cars="Mustang";
my_cars="Station Wagon";
my_cars="SUV";
alert(my_cars.length);
|
by: VK |
last post by:
Hello,
In my object I have getDirectory() method which returns 2-dimentional
array
(or an imitation of 2-dimentional array using two JavaScript objects
with auto-handled length property - please let's us do not go into an
"each dot over i" clarification discussion now - however you want to
call - you call it ;-)
array contains records of all files in the current dir.
array contains records of all subs in the current dir
|
by: yeti349 |
last post by:
Hi, I'm using the following code to retrieve data from an xml file and
populate a javascript array. The data is then displayed in html table
form. I would like to then be able to sort by each column. Once the
array elements are split, what is the best way to sort them? Thank you.
//populate data object with data from xml file.
//Data is a comma delimited list of values
var jsData = new Array();
jsData = {lib: "#field...
|
by: JJA |
last post by:
I'm looking at some code I do not understand:
var icons = new Array();
icons = new GIcon();
icons.image = "somefilename.png";
I read this as an array of icons is being built.
An element of the array is an object itself but what is this syntax of
the consecutive double quotes inside the brackets ?
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |