473,789 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
+ 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_exist s($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 4786
On 4 Okt., 17:18, Mads Lee Jensen <madsleejen...@ hotmail.comwrot e:
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_exist s($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($sP rop, $this->arrProps)){
echo "Found Class Property '$sProp'<br>";
}
else{
echo "Invalid Prop '$sProp'<br>";
$this->{$sProp} = $Val; // define it ;)
}
}

protected $arrProps = array("FirstNam e", "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
2553
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; function __construct(){ $this->meta = new Meta(array of data); } }
8
1924
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 when invoked explicitly (eg obj.__iadd__(3)) but the method seems not to be associated with the corresponding operator (ie obj += 3 does NOT work). What am I likely to be doing wrong ?
2
2063
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 achieve this, I need to analyze python code which will look somethink like this: def foo(arg_dict): return arg_dict + bar(arg_dict)
0
1726
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 > add23 = adder.__get__(23) > add42 = adder.__get__(42) > print add23(100), add42(1000) > 123 1042
5
5538
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 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...
16
2608
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 previous python experiences: magic names (I felt almost as sad as when I discovered the strange pink worms that eat you in nethack, not to mention the mind flayers - I really hate them). I guess all programming languages have magic names to some...
2
2583
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
1563
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 function returns TRUE.
9
16158
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 been able to find is the python-magic module at http://hupp.org/adam/hg/python-magic/. Is this "THE" python-magic module. (It seems to be to me, but obviously I don't know. :)
0
9656
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10374
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...
0
10177
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10121
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,...
1
7519
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 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...
0
5404
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
5539
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4076
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
3
2898
bsmnconsultancy
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...

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.