Jump to content

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 :)

 

 

Looks like PHP is passing your $order object by reference to the $data class. Some versions of PHP will do this implicitly I believe. And yes, you are violating the laws of encapsulation. ;D

 

(not really)

Ed

 

Care to expand?

Not really, no ;)

 

As per http://www.php.net/manual/en/migration5.oop.php, objects are passed about by reference instead of by value. CaptainChainsaw was wondering why his code was working when at a glance it appears that it shouldn't.

 

Cheers

Ed

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.