473,789 Members | 2,254 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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($arr ay){
$this->_values = $array;
$this->_modified = FALSE;
}
function __set($name,$va lue){
$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 2553
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($ar ray){
$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.anti spam.net> wrote in message
news:LY******** ************@co mcast.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***@phppeanu tsREMOVE-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
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...
3
1796
by: K.K. | last post by:
Consider the following code: >>>>>>>>>>> // Define an empty class public class ZorgleCollection : Dictionary<string, Zorgle> { } // Somewhere outside ZorgleCollection:
3
41539
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 am not clear on the disadvantages. I have had a look on the Web and in newsgroups but couldn't find much. As time is running out, I thought I would post here and hope that someone would reply. Thanks Rob
5
1683
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 can not be modified after it was created. Up to here everything is fine. The problem is when I want to get clever with data storage. My program has an array of "DataBuffers" that gets pre-allocated. If I want to use that memory instead of...
47
3372
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) {return
32
4229
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 files; but now and then I go on an opaque type joyride, and create minimalist header files that define very clean interfaces. The problem with that is that it prevents some optimizations:
2
7641
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 mentioning the class members(functions, typedefs, data) under the access control labels : public, protected, private. Encapsulation means providing the implementation of class member
1
4786
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 { public $fornavn = ''; private $efternavn = '';
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.
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
9499
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
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,...
0
9969
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
8995
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
6750
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
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?

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.