top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Building variable getter-method in PHP?

+1 vote
377 views

I'd like to do something like this:

if (method_exists($object, 'get' . ucfirst($property))) {
 $children = $object->{'get' . ucfirst($property)};
}

but this is not working unfortunately $children is always NULL although the getter exits. Is there a another way to build a dynamic getter?

posted May 29, 2014 by Deepti Singh

Looking for an answer?  Promote on:
Facebook Share Button Twitter Share Button LinkedIn Share Button
make sure your getter class returns something!

Similar Questions
+2 votes

I'm having a problem getting my objects out of the $SESSION variable. I am storing an ordinal array of associate arrays where one of the associate array elements is an object. Unfortunately, when I pull it back out, the object is "broken" with "__PHP_Incomplete_Class" errors.

From what I can read this has something to do with 'serialization' - i.e., the emerging object must be unserialized (http://php.net/manual/en/function.unserialize.php ). However, I can't get it to work.

Any tips on how that is done, or what I should do instead?

This program is being developed on a testing server that can then be put on a production server.

Here are my server specs:
Apache Version : 2.2.17
PHP Version : 5.3.3
MySQL Version : 5.5.8

0 votes

I've tried the following code but the $call variable is not available in the pcntl_fork child. Searching the web, I've found posts that claim that all variables defined before pcntl_fork are copied to the child. I've also found posts that claim that no variables are passed/copied to the child. So, which of them is true and how should I correct my code to make it work?

 $call = str_replace('@@', '&', $_GET['call']);

 if ($pid = pcntl_fork())
 {
     $previousCalls=file("calls.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
     if (!in_array($call, $previousCalls))
     {
         file_put_contents("calls.txt", $call."\n", FILE_APPEND);
     }
 }
 else
 {
     function displayUrl($url)
     {
         $ch = curl_init($url);
         curl_setopt ( $ch, CURLOPT_HEADER, 1);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         $curlResult = curl_exec($ch);
         curl_close($ch);
         list($headers,$content) = explode("\r\n\r\n",$curlResult,2);
         foreach (explode("\r\n",$headers) as $hdr)
         {
             if ($hdr != 'Transfer-Encoding: chunked')
             {
                 header($hdr);
             }
         }
         echo $content;
     }
     displayUrl($call);
 }
...