473,405 Members | 2,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

I fell into a strange trap today. Add non-existent properties to non-existent class!

<?php
/*
Making up class properties on the fly!
--------------------------------------

PHP version 4.3.5

An instance of a class can have properties added on the fly.

This behaviour tripped me up when I got my objects a bit mixed up
where $wrongClassInstance->aVar='foo' should have been
$correctClassInstance->aVar='foo'.
No error was reported. PHP happily added aVar to the properties of
$notThisOne being an instance of a class with no $aVar.

Q1: Does this happen in PHP 5?
Q2: Is there a way to trap or turn this off?
*/
class A {
var $foo; // foo is a legitimate variable

function TryThis(){
$this->foo = $this->bar; // bar is a bastard variable (not
declared)
}
}

#================================================= =====================

$a = new A;
$a->foo = 'foo';
print("FOO $a->foo<br>"); // so far so normal

$a->bar = 'bar'; // making this up as we go along!
print("BAR $a->bar<br>"); // OK!

var_dump($a);

$a->TryThis(); // and this works!
print("<br>??? $a->foo<br>");

#================================================= =====================

print('<hr>');
$b = new A;
$b->foo='FOO';
$b->TryThis(); // this fails
var_dump($b);
#================================================= =====================

print('<hr>');
$c = NULL; // now it gets really strange
$c->foo = 'foo'; // this works!!
print("FOO $c->foo<br>");
var_dump($c);
print(phpversion());
?>

--
PETER FOX Not the same since the poster business went to the wall
2 Tees Close, Witham, Essex. pr*****@eminent.demon.co.uk
Gravity beer in Essex <http://www.eminent.demon.co.uk>

Jul 17 '05 #1
2 2012

"Peter Fox" <bi*@eminent.demon.co.uk> wrote in message
news:A9**************@eminent.demon.co.uk...
<?php
/*
Making up class properties on the fly!
--------------------------------------

PHP version 4.3.5

An instance of a class can have properties added on the fly.

This behaviour tripped me up when I got my objects a bit mixed up
where $wrongClassInstance->aVar='foo' should have been
$correctClassInstance->aVar='foo'.
No error was reported. PHP happily added aVar to the properties of
$notThisOne being an instance of a class with no $aVar.

Q1: Does this happen in PHP 5?
Q2: Is there a way to trap or turn this off?
*/


In PHP5 you can add a __set() and a __get() method to track access to
undeclared properties.

As far as I know there's isn't a way you can make PHP spit out an error or a
warning when an undeclared property is set.
Jul 17 '05 #2
On Tue, 25 Jan 2005 21:07:25 +0000, Peter Fox
<bi*@eminent.demon.co.uk> wrote:
An instance of a class can have properties added on the fly.


That's standard behavour. In fact, for PHP4 I almost never declare
class properties for that reason. Properties are just regular
variables in this way; you can create them just by assigning to them.

PHP5 has _get() and _set() which will allow you to raise an exception
or error on undeclared properties. I also believe PHP5 also does not
do automatic promotion to objects -- which would prevent your second
example:

$a = NULL;
$a->Var = 'hello'

This won't work in PHP5 because $a won't be automatically converted
into an object from null. This auto conversion is an old feature from
the PHP3 days where you could create an object just by assigning a
property to an undeclared (null) variable.

Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jay Donnell | last post by:
My script stopped working today after working perfectly for a month. It still works perfectly on our test server just not on our live server. The form that I use is this: <form...
2
by: inonzuk | last post by:
why strange you ask? here goes: I have been working with Php and Mysql for a week without any problems. Today I tried executing a php page that connects to mysql and creates a new database, I...
24
by: brian.bird | last post by:
Can anyone explain the behaviour of python when running this script? >>> def method(n, bits=): .... bits.append(n) .... print bits .... >>> method(1) >>> method(2)
3
by: Andrew Mayo | last post by:
(note: reason for posting here; browser helper object is written in C++; C++ developers tend to know the intricacies of message handling; this looks like a Windows messaging issue) Microsoft...
18
by: Mantorok Redgormor | last post by:
What does a trap representation mean in the standard? And how can ~0 cause a trap representation? Could someone point out the relevant sections in the standard?
1
by: Chris | last post by:
Hey, I have a DataGrid that allows for a combobox in a column through a inherited DataGridColumnStyle and a inherited ComboBox. I have 3 of these columns in a row. The overall problem is that...
1
by: Tino Lange | last post by:
Hi! I'm surprised about the following code, maybe you can give me a hint whether that's bug or feature? I'm just trying to convert local time to GMT and one method gives a strange result: ...
32
by: REH | last post by:
OK, I was at a code review today and got asked a strange questions (at least I thought it was). I pointed out that the naming convention they were using for macros clashed with one of the reserved...
4
by: | last post by:
Im getting that error (It is strange. I Run my programme step by step pressing f11. im looping SqlCommand in a while loop. The First step runs but when a enter 2nd step in my loop it returns error...
4
by: schnupfy | last post by:
Hi, I am not used to python and I am wondering about this thing: If I execute this from the shell: /root/mk/services.py 192.168.1.101 critical "192.168.1.101 192.168.1.101...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
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...
0
agi2029
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,...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.