473,396 Members | 1,757 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,396 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 2542
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...

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.