Jump to content

[SOLVED] Am I violating the laws of encapsulation?


Recommended Posts

Hi all,

 

I have a problem with my code, it does work but I'm a bit concerned that it is working, as I expect that it shouldn't be.

 

All the classes in my code have a private member called $members=array(); which I would expect to be private to that class.

 

When I run the following code................

 

 

$orders=new orders();
$data=new cleanser();

foreach($orders as $order){
$data->__set('Order', $order);	
             $data->clean();
$filedata = $order->getName()."\t".$order->getAddress();
}

 

 

............. the data is cleansed and properly assigned to $filedata.

 

What I'm wondering is shouldn't I need to call:

 

$data->__get('Order');

 

in order to "get" the cleansed object?  When calling $order->getName(); why would the "Name" be displayed as I expect it if the cleansed object hasn't been returned?  Have I done something wrong if I don't need to call the __get() method?

 

I *think* I've designed my classes well enough but I could be wrong.  I know it would probably help to have the classes posted here but I don't have access to them right now.

 

 

Cheers for any tips or suggestions.

 

 

CaptainChainsaw :)

 

It sounds like getName is your "get" method, which allows you to access private properties in a class I beleive. I could be wrong...

 

I don't think you can directly access those properties like so:

 

$data->members();

 

But if your getName() is getting the data and then outputting it, that's probably why you're seeing the data??

Hi all, thanks for your replies.

 

As far as I can remember the "getName()" method is structured like so:

public function getName(){
    return $this->members['Order']['name'];
}

 

 

As the modified $orders object hasn't been returned by the $data object why is it showing the "cleansed" data?  I would expect echo $order->getName(); to show the unmodified data as the object has only been set and "cleaned" by the $data object and NOT returned.

 

 

Hope that makes a little more sense (if you needed it).

 

 

 

Cheers again,

 

 

CaptainChainsaw :)

You should be requiring  all interaction to be performed through an object's methods.  They are 'get' and 'set' methods (accessors and mutators) in your class that get and set data.  From the information provided it looks like your code obeys the rules of encapsulation.

Hi Maq, I agree, all interaction is done with getters and setters, I just don't understand why I don't need to do something like this within the loop:

 

$data->__set('Order', $order);

            $data->clean();

            $order=$data->__get('Order');

 

 

hmmm .....

 

 

Ah well at least my code works!

 

 

 

Thanks again,

 

CaptainChainsaw :)

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.