473,468 Members | 1,627 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

magic method's __set().

is it the __set() purpose to be called if a property exists in the
object but is out of the scope.
or is it only to be used for 'undefined instance properties'.

test:

class Magic_Set
{
public $fornavn = '';
private $efternavn = '';

public function __set($varName, $value)
{
if (property_exists($this, $varName)) {
echo 'edit ['.$varName.'='.$this->$varName.']
to ['.$varName.'='.$value.']';
}
else {
echo 'defining ['.$varName.'='.$value.']';
}
$this->$varName = $value;
}
}

$test = new Magic_Set();

$test->fornavn = 'mads';
$test->efternavn = 'jensen';
$test->mellemnavn = 'lee';

Oct 4 '07 #1
1 4772
On 4 Okt., 17:18, Mads Lee Jensen <madsleejen...@hotmail.comwrote:
is it the __set() purpose to be called if a property exists in the
object but is out of the scope.
or is it only to be used for 'undefined instance properties'.

test:

class Magic_Set
{
public $fornavn = '';
private $efternavn = '';

public function __set($varName, $value)
{
if (property_exists($this, $varName)) {
echo 'edit ['.$varName.'='.$this->$varName.']
to ['.$varName.'='.$value.']';
}
else {
echo 'defining ['.$varName.'='.$value.']';
}
$this->$varName = $value;
}

}

$test = new Magic_Set();

$test->fornavn = 'mads';
$test->efternavn = 'jensen';
$test->mellemnavn = 'lee';
Hi

The __set() magic function is called whenever an undefined class
variable is set

<?
class Foo
{
public function __set($sProp, $Val)
{
if(in_array($sProp, $this->arrProps)){
echo "Found Class Property '$sProp'<br>";
}
else{
echo "Invalid Prop '$sProp'<br>";
$this->{$sProp} = $Val; // define it ;)
}
}

protected $arrProps = array("FirstName", "LastName");

public $Test = "";
}

$Foo = new Foo();
$Foo->FirstName = "Hugo";

// __set gets called
$Foo->tName = "Hugo";

// tName is defined
echo $Foo->tName;

// no __set gets called
$Foo->tName = "Hugo3";

echo $Foo->tName;

// no __set gets called because of object member
$Foo->Test= "no set calles";
?>

hope this helps

Oct 4 '07 #2

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

Similar topics

3
by: Pingveno | last post by:
I'm writing a web application that requires meta data in some objects. I'm trying to make to API as clean as possible. Would it be better to have something like this: class Mama { public $meta;...
8
by: Jacek Generowicz | last post by:
I am writing an extension type, and wish to add some magic methods which are not catered for by the tp_<whatever> slots (eg tp_init -> __init__) in PyTypeObject. My methods seem to work correctly...
2
by: Jan Burgy | last post by:
Hi everyone, I am trying to convince my managers that python can replace the outdated and soon no-longer maintained proprietary system (Tool for Calculator Design) we use here. In order to...
0
by: Michael Spencer | last post by:
Wow - Alex Martelli's 'Black Magic' Pycon notes http://www.python.org/pycon/2005/papers/36/pyc05_bla_dp.pdf include this gem: > Functions 'r descriptors > def adder(x, y): return x + y > ...
5
by: Jon Maz | last post by:
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...
16
by: per9000 | last post by:
Hi, I recently started working a lot more in python than I have done in the past. And I discovered something that totally removed the pretty pink clouds of beautifulness that had surrounded my...
2
by: jyck91 | last post by:
i have done the magic square: #include <stdio.h> #include <stdlib.h> #include <string.h> #define SIZE 13 main() { FILE *fp; int i, j, n, row, column;
4
by: turnitup | last post by:
What is the easiest way of testing whether a call to __set() has been successful? At the moment doing $success = ($foo->bar = "fred"); echo $success returns "fred", even though the __set...
9
by: Larry Hale | last post by:
I've heard tell of a Python binding for libmagic (file(1) *nixy command; see http://darwinsys.com/file/). Generally, has anybody built this and worked with it under Windows? The only thing I've...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.