Sign in to follow this  
Followers 0
TechCoder

Object Oriented PHP - need some 'translation' help...

6 posts in this topic

I'm working on a program for a client and am (reluctantly) working with an object-oriented program.

Things are going 'well', though I just got a response that I am not even close to understanding and searching around, trying to learn how to read this stuff has lead nowhere (and certainly is killing my productive time on this).  

So, I thought of this forum and bet that someone can/will give me a pointer to figure it out.

print_r(get_object_vars($this));echo "<br>";
    Array ( [baseService] => PermissionService Object ( [_store:PermissionService:private] => ResourcePermissionStore Object ( [_scheduleUserRepository:ResourcePermissionStore:private] => ScheduleUserRepository Object ())[_allowedResourceIds:PermissionService:private] => Array ( [0] => 59 [1] => 60 [2] => 65 [3] => 69 [4] => 80 [5] => 104 [6] => 115 [7] => 121 [8] => 159 [9] => 164 [10] => 166 [11] => 255 [12] => 287 [13] => 304 ) ))

above shows what I did to get the response and what the response brings back.

What I need to know is (in a 'simple' fashion that us old-time, procedural style folks can understand) 

JUST HOW DO I ACCESS THE ARRAY OF NUMBERS (which is all I really care about in this instance)

What I'd like, is, for instance, the code to access #5 in the array (result 104) like;

echo $this->whateverthe::heck->I;need->todo[5]

(if you get the idea that I HATE object-oriented code {and understand nearly nothing about it}, well, you read me right!)

btw, thanks in advance!

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Holy crap.  I see why PHP gets such a bad wrap.  If I understand this correctly, the law of demeter has been broken in the first degree.  The punishment is death.

Converting this to procedural might be long past the point of no return. 

My first question I guess is why is a static array being declared?

Try this and hope for the best:

print_r(get_object_vars($this));

echo "<br>";

$foo = Array( 
        [baseService] => 
          PermissionService Object( 
            [_store:PermissionService:private] => 
              ResourcePermissionStore Object( 
                [_scheduleUserRepository:ResourcePermissionStore:private] => 
                  ScheduleUserRepository Object ())
                    [_allowedResourceIds:PermissionService:private] => 
                      Array( 
                        [0] => 59 [1] => 60 [2] => 65 [3] => 69 [4] => 80 [5] => 104 [6] => 115 [7] => 121 [8] => 159 [9] => 164 [10] => 166 [11] => 255 [12] => 287 [13] => 304)))   
    
$foo[5];

Edit: Ok, I think I did misunderstand seriously.

Try this: $foo = get_object_vars($this);

Edited by jaberwacky

Share this post


Link to post
Share on other sites

That complex array stuff was the result of print_r(...), not code.

Accessing starts with $this, that's about as far as I can tell without looking at what get_object_vars returns.

Share this post


Link to post
Share on other sites

I think what he was saying is that it returns this hot ratchet mess:

Array ( [baseService] => PermissionService Object ( [_store:PermissionService:private] => ResourcePermissionStore Object ( [_scheduleUserRepository:ResourcePermissionStore:private] => ScheduleUserRepository Object ())[_allowedResourceIds:PermissionService:private] => Array ( [0] => 59 [1] => 60 [2] => 65 [3] => 69 [4] => 80 [5] => 104 [6] => 115 [7] => 121 [8] => 159 [9] => 164 [10] => 166 [11] => 255 [12] => 287 [13] => 304 ) ))

Share this post


Link to post
Share on other sites

Yes, but you wouldn't assign it to a variable. Your code may work if you did $foo = get_object_vars($this); 

Share this post


Link to post
Share on other sites

Thanks for the replies!

$foo = get_object_vars($this); 
    echo __LINE__ . " foo is <br>";print_r($foo); echo "<br>";

gives

 

foo is 
Array ( [baseService] => PermissionService Object ( [_store:PermissionService:private] => ResourcePermissionStore Object ( [_scheduleUserRepository:ResourcePermissionStore:private] => ScheduleUserRepository Object ( ) ) [_allowedResourceIds:PermissionService:private] => Array ( [0] => 59 [1] => 60 [2] => 65 [3] => 69 [4] => 80 [5] => 104 [6] => 115 [7] => 121 [8] => 159 [9] => 164 [10] => 166 [11] => 255 [12] => 287 [13] => 304 ) ) ) 

, so not much closer.

However, along with the messages from this forum about your replies, there was a reply from the designer of the 'hot ratchet mess' :muttley: ... ;) - earlier in the code (along with a thousand other 'one line' functions, which I still don't understand how they actually DO anything....{or why they can't just lay it out - yeah, like procedural! :} ) he had

public function CanAccessResource(IPermissibleResource $resource, UserSession $user)
    {

which made it work (somewhat) with 

$this_resource = get_object_vars($resource);

and now I can access the data I need at this moment (which is the 'resource_id' he sent me to check the permission on)

echo __LINE__ . " " . $this_resource['resource_id'] . "<br>";

if I can access the full array that would save some time later (needed in a later process again anyway - would be nice to have it instead of going to look things up again), though, carrying it around (and, of course, 'translating' it in the first place) might be as much trouble as just going to do a direct db dip later.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0