473,626 Members | 3,389 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 $wrongClassInst ance->aVar='foo' should have been
$correctClassIn stance->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(phpversio n());
?>

--
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.dem on.co.uk>

Jul 17 '05 #1
2 2020

"Peter Fox" <bi*@eminent.de mon.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 $wrongClassInst ance->aVar='foo' should have been
$correctClassIn stance->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.de mon.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
1774
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 enctype="multipart/form-data" action="" method="post"> <input type="hidden" name="MAX_FILE_SIZE" value="3000000" /> Send this file: <input name="userfile" type="file" /> <input type="submit" value="Send File" /> </form>
2
2368
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 got this error: "Fatal error: Call to undefined function mysql_connect() in C:\Program Files\Apache Group\Apache2\test\_debug_tmp.php on line 3". I did some reading on the net and checked all my configuration, and it
24
2019
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
2299
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 very kindly make available a DLL (the browser helper DLL) which allows you to trap and disable the context menu (right mouse click) and various 'built-in' browser accelerators (such as ctrl+P which brings up the print dialogue, and F5, which will...
18
9047
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
1124
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 when I hit TAB on the first of 3 of these columns the cursor moves into the second and then immideantly leaves and moves to the third box. if you leave the second one it moves to a new line (through the 3rd one). I'm trying to figure out why...
1
2619
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: ###################### > #! /usr/bin/env python >
32
1949
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 forms for identifies in the standard. After some shock and disbelief, the lead of the project agreed to change to convention. One of the engineers piped up and asked, "Do we really want to accept the standard?" I didn't know how to answer...
4
1361
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 ? ) any idea ? System.Data.SqlClient.SqlException: Procedure or function sp_adduser has too many arguments specified My Stored Procedure
4
1510
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 SNMPv2-MIB::sysUpTime.0 14:13:02:57.06 SNMPv2- MIB::snmpTrapOID.0 SNMPv2-SMI::enterprises.789.0.13 SNMPv2- SMI::enterprises.789.0.2"cfCannotTakeover == 1 priority == critical"
0
8202
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,...
0
8707
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8366
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,...
0
8510
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 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...
0
7199
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, 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...
0
5575
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();...
0
4093
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...
0
4202
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2628
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.