473,289 Members | 1,917 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,289 software developers and data experts.

Encapsulation vs. __set and __get

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);
}
}

class Meta {
private $_values;
private $_modified;
function __contruct($array){
$this->_values = $array;
$this->_modified = FALSE;
}
function __set($name,$value){
$this->_modified = TRUE;
$this->_values[$name] = $value;
}
function __get ($name){
return $this->_values[$name];
}

}

-or-

class Mama {
public $_meta = array('name' => 'value';
function getMeta($name){
return $this->_meta[$name];
}
function setMeta($name,$value){
$this->_meta[$name] = $value;
}
function delMeta($name){
unset($this->_meta[$name])
}

}

I haven't done much OOP before, so I don't know what is "standard
practice." The project is for a small school newspaper, so it's more of
a programming excercise for me. What is more OOP correct?

-Pingveno
Jul 17 '05 #1
3 2533
Pingveno wrote:
I haven't done much OOP before, so I don't know what is "standard
practice." The project is for a small school newspaper, so it's more
of a programming excercise for me. What is more OOP correct?


Using __get and __set methods, only means that you want to use overloading
to enable calls like:

$mama = new Mama;
$mama->meta->foo = 'bar';
print $mama->meta->foo;

In this example you can see the first problem: because overloading is only
supported in the Meta class, you are forced to use the multiple object
operator syntax to access the $_values property of the Meta class. This
causes your code to be less readable and the usage of your class less
obvious.

To improve this, you could move overloading from the Meta class to the Mama
class. The overall design of the Mama class determines whether this is a
good choice and the way you want to use it determines if you should use
overloading at all.

As an alternative, you could design the Meta class as an interface, with an
implementation in the Mama class and clearly defined methods:

interface Meta {
public function setMeta($name, $value);
public function getMeta($name);
}

class Mama implements Meta {
private $_values;
function __construct($array){
$this->_values = $array;
}

function getMeta($name) {
return $this->_values[$name];
}

function setMeta($name, $value) {
$this->_values[$name] = $value;
}
//.... other methods specific to the Mama class
}
JW

Jul 17 '05 #2
"Pingveno" <pi**************@comcast.antispam.net> wrote in message
news:LY********************@comcast.com...
I haven't done much OOP before, so I don't know what is "standard
practice." The project is for a small school newspaper, so it's more of
a programming excercise for me. What is more OOP correct?


The standard practice, I dare say, is not to make a distinction between
"data" and "meta data," as that adds to the complexity of the interface. The
caller doesn't need to know the nature of what it is accessing. Both should
be present plainly as properties. Whatever differences between the two type
should be handled within the class.
Jul 17 '05 #3
"Henk Verhoeven" <ne***@phppeanutsREMOVE-THIS.org> wrote in message
news:d3**********@news2.zwoll1.ov.home.nl...
Actually i do not see any meta data here (IMHO meta data is data about
data). Pingveno's second example i would call an associative multi value
property named 'meta' and the only thing meta about this property would
be its name.


Personally I dislike the term "meta data." Data is what a computer deals
with. Any piece of data could be about another piece. The author of an
article, for example, could be considered meta data of the article.

In any event, just because something is meta data doesn't mean you have to
model it that way.
Jul 17 '05 #4

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

Similar topics

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...
3
by: K.K. | last post by:
Consider the following code: >>>>>>>>>>> // Define an empty class public class ZorgleCollection : Dictionary<string, Zorgle> { } // Somewhere outside ZorgleCollection:
3
by: enchantingdb | last post by:
I have an exam tomorrow that covers the perceived advantages and disadvantages of object oriented programming, in particular polymorphism, inheritance and encapsulation. I know the advantages but...
5
by: jmsantoss | last post by:
Hi, This is a design question. I have a class named "DataBuffer" that stores some data. After "DataBuffer" is created it can not be modified. All the methods of "DataBuffer" are const as data...
47
by: Roger Lakner | last post by:
I often see operator implemented something like this: class Foo { ... }; class FooList { public: const Foo& operator (unsigned index) const {return array;}; Foo& operator (unsigned index) ...
32
by: bluejack | last post by:
Ahoy: For as long as I've been using C, I've vacillated on the optimal degree of encapsulation in my designs. At a minimum, I aggregate data and code that operate on that data into classlike...
2
by: subramanian100in | last post by:
Is my following understanding correct ? Data abstraction means providing the interface - that is, the set of functions that can be called by the user of a class. Information hiding means...
1
by: Mads Lee Jensen | last post by:
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 {...
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.