php-general Digest 8 May 2006 04:46:18 -0000 Issue 4115

Topics (messages 235690 through 235699):

Passing an indefinite number of parameters by reference
        235690 by: Chris Jenkinson
        235691 by: Jochem Maas
        235693 by: Chris Jenkinson
        235694 by: Jochem Maas
        235695 by: Eric Butera
        235698 by: Tom Rogers

Re: Echo a value from an arrays position
        235692 by: Jochem Maas

Convert from jpg to gif ... change dpi...
        235696 by: Gustav Wiberg
        235697 by: Rory Browne

Socket Functions in PHP
        235699 by: Oliver John V. Tibi

Administrivia:

To subscribe to the digest, e-mail:
        [EMAIL PROTECTED]

To unsubscribe from the digest, e-mail:
        [EMAIL PROTECTED]

To post to the list, e-mail:
        [email protected]


----------------------------------------------------------------------
--- Begin Message ---
Hi,

Currently I have a function which accepts a limited number of parameters:

call_function($function_name, &$var_1, &$var_2);

I wish to modify the function to accept an indefinite number of parameters, which may or may not be references.

The function call_function() then calls the function specified in $function_name, with the variables passed as parameters to that function.

Is this possible? Thanks for any help which can be offered.

Chris

--
Chris Jenkinson
[EMAIL PROTECTED]

"Mistrust all in whom the impulse to punish is powerful."
 -- Friedrich Nietzsche, Thus Spoke Zarathustra

--- End Message ---
--- Begin Message ---
Chris Jenkinson wrote:
Hi,

Currently I have a function which accepts a limited number of parameters:

call_function($function_name, &$var_1, &$var_2);

I wish to modify the function to accept an indefinite number of parameters, which may or may not be references.

The function call_function() then calls the function specified in $function_name, with the variables passed as parameters to that function.

Is this possible? Thanks for any help which can be offered.

I believe it is - BUT it won't be easy, also I think you can only really do this
properly in php5 using a shedload of reflection functionality:

http://php.net/manual/en/language.oop5.reflection.php

although I wonder whether you shouldn't be re-evaluating what it is
your trying to do because I get the impression it's a whole load of work
for probably little payoff (consider that using alot of indirection in your
code will make it harder to understand/read and therefore harder to 
debug/maintain).
why not consider the possibility that all these 'by reference' args could be 
items
in a single 'by reference' array?

btw: have you met call_user_func_array() yet? (not that it
would solve the problem because it doesn't 'do' references) - also,
it's manual page offers a hack for the reference problem your working on.


Chris


--- End Message ---
--- Begin Message ---
Jochem Maas wrote:
although I wonder whether you shouldn't be re-evaluating what it is
your trying to do because I get the impression it's a whole load of work
for probably little payoff (consider that using alot of indirection in your
code will make it harder to understand/read and therefore harder to debug/maintain). why not consider the possibility that all these 'by reference' args could be items
in a single 'by reference' array?

Yes, that would be possible, however that would involve an API change - as now functions would have to get their variables out of an array rather than just a normal variable defined in the function. At the moment I've just decided to add more $var_x parameters.

btw: have you met call_user_func_array() yet? (not that it
would solve the problem because it doesn't 'do' references) - also,
it's manual page offers a hack for the reference problem your working on.

Yes, I briefly considered using it. Another method I tried was building a list of parameters and using eval() to call the function. Since I can't get the variables by reference, however, I've stuck with just passing $var_1, $var_2, etc. to the function.

Thanks for your comments. I think the amount of effort I would spend learning how to use the reflection API compared to just using a defined number of variables labelled in sequence is not worth it. If there was another way, perhaps...

Chris

--
Chris Jenkinson
[EMAIL PROTECTED]

"Mistrust all in whom the impulse to punish is powerful."
 -- Friedrich Nietzsche, Thus Spoke Zarathustra

--- End Message ---
--- Begin Message ---
Chris Jenkinson wrote:
Jochem Maas wrote:

although I wonder whether you shouldn't be re-evaluating what it is
your trying to do because I get the impression it's a whole load of work
for probably little payoff (consider that using alot of indirection in your code will make it harder to understand/read and therefore harder to debug/maintain). why not consider the possibility that all these 'by reference' args could be items
in a single 'by reference' array?


Yes, that would be possible, however that would involve an API change - as now functions would have to get their variables out of an array rather than just a normal variable defined in the function. At the

extract() and compact() would be your friends.

moment I've just decided to add more $var_x parameters.

that smells like bad design (but then again you should see some of my code ;-)


btw: have you met call_user_func_array() yet? (not that it
would solve the problem because it doesn't 'do' references) - also,
it's manual page offers a hack for the reference problem your working on.


Yes, I briefly considered using it. Another method I tried was building a list of parameters and using eval() to call the function. Since I can't get the variables by reference, however, I've stuck with just passing $var_1, $var_2, etc. to the function.

Thanks for your comments. I think the amount of effort I would spend learning how to use the reflection API compared to just using a defined number of variables labelled in sequence is not worth it. If there was another way, perhaps...

i can say with confidence 'no, your **** out of luck'.

Chris


--- End Message ---
--- Begin Message ---
On 5/7/06, Chris Jenkinson <[EMAIL PROTECTED]> wrote:
Hi,

Currently I have a function which accepts a limited number of parameters:

call_function($function_name, &$var_1, &$var_2);

I wish to modify the function to accept an indefinite number of
parameters, which may or may not be references.

The function call_function() then calls the function specified in
$function_name, with the variables passed as parameters to that function.

Is this possible? Thanks for any help which can be offered.

Chris

--
Chris Jenkinson
[EMAIL PROTECTED]

"Mistrust all in whom the impulse to punish is powerful."
  -- Friedrich Nietzsche, Thus Spoke Zarathustra

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


One thing you could do is (assuming it's php4)

<?php
function printArray($array) {
   echo '<pre>';
   print_r($array);
   echo '</pre>';
}

function call_function($name, &$params) {
   call_user_func($name, $params);
}



function test(&$params) {
   extract($params, EXTR_REFS);
   printArray($myarray1);
   printArray($myarray2);
   // prove this is a reference by modifying it
   $myarray2 = array('one', 'two', 'three');
}

$myarray1 = array(3, 6, 9);
$myarray2 = array('two', 'four', 'six');
$myParams['myarray1'] =& $myarray1;
$myParams['myarray2'] =& $myarray2;

call_function('test', $myParams);

// myarray2 now has the values set in test
printArray($myarray2);

?>


Output:

Array
(
   [0] => 3
   [1] => 6
   [2] => 9
)

Array
(
   [0] => two
   [1] => four
   [2] => six
)

Array
(
   [0] => one
   [1] => two
   [2] => three
)


This is a bit of a pain though. :)

--- End Message ---
--- Begin Message ---
Hi,

Monday, May 8, 2006, 4:20:11 AM, you wrote:
CJ> Hi,

CJ> Currently I have a function which accepts a limited number of parameters:

CJ> call_function($function_name, &$var_1, &$var_2);

CJ> I wish to modify the function to accept an indefinite number of 
CJ> parameters, which may or may not be references.

CJ> The function call_function() then calls the function specified in 
CJ> $function_name, with the variables passed as parameters to that function.

CJ> Is this possible? Thanks for any help which can be offered.

CJ> Chris

Here is a cutdown version of a class loader I use, It works for php4 and
php5 and with references. It should be easy to modify.

<?php
function &load_class($class_name,$arg1,$arg2,$arg3){
  $num_args = func_num_args();
  $arg_list = func_get_args();
  
  $vars = '$return =& new '.$class_name.'(';
  if($num_args > 1) {
    for ($i = 1; $i < $num_args; $i++) {
      $vars .= ($i > 1)? ',':'';
      $varname = 'variable'.$i;
      $$varname =& $arg_list[$i];
      $vars .= "\$$varname";
    }
  }
  $vars .= ');';
  eval($vars);
  return $return;
}
?>

-- 
regards,
Tom

--- End Message ---
--- Begin Message ---
Dave Goodchild wrote:
This may clarify - in php, integer and associate arrays are created
arbitrarily, ie keys can be numbers or strings. So, either create an array
like this:

array('1' => 'first element',
       '2' => 'second element');

if I'm not mistaken the above example will result in an array with
2 *numeric* keys because any strings keys that php that are numeric
(according to the logic of is_numeric()) will be seen as numeric keys.

so the above example could lead to even more confusion!


and call by the key!

On 04/05/06, Jonas Rosling <[EMAIL PROTECTED]> wrote:


Is there any way to call for an element value in an array by the position?
Like position 2 in the array and not the key name.

// Jonas

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




--
http://www.web-buddha.co.uk

dynamic web programming from Reigate, Surrey UK (php, mysql, xhtml, css)

look out for project karma, our new venture, coming soon!


--- End Message ---
--- Begin Message ---
Hi there!

Is there any way of converting a jpg to gif and change dpi on the fly?

Best regards
Gustav Wiberg

--- End Message ---
--- Begin Message ---
imagemagick?

On 5/7/06, Gustav Wiberg <[EMAIL PROTECTED]> wrote:
>
> Hi there!
>
> Is there any way of converting a jpg to gif and change dpi on the fly?
>
> Best regards
> Gustav Wiberg
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--- End Message ---
--- Begin Message ---
Hi all,

I suspect I'm having problems with socket functions within my PHP application running on Red Hat 2.4.21-4.EL #1/PHP 4.3.2/Apache 2.0.46.

Below is a sample code listing:

================================================================

function sendRequest($strRequest) {
        $parser = NULL;
        $logman = new LogManager();
        
        $resource = socket_create( AF_INET, SOCK_STREAM, SOL_TCP );
        $logman->append("Connecting to OLS Server...");
        $socket_conn = socket_connect( $resource, $serverIP, $serverPort );
        if( $socket_conn ) {
$logman->append("Connected to {$serverIP}:{$serverPort}. Sending XML request...");
                socket_write( $resource, $strRequest );
                $logman->append("XML request sent. Waiting for XML 
response...");
                $document = "";
                $data = socket_read( $resource, 1024 );
                
                while ($data != "") {
                        $document .= $data;
                        $data = socket_read( $resource, 1024 );
                }
                $logman->append("XML response received.");
                
                $parser = new OlsResponseParser;
                // parse the received document using our parser
                $parser->parse($document);
        }
        
        if ($parser != NULL) {
                return $parser->getOlsResponse();
        }
        
        return $parser;
}

================================================================
The following listing is from the logs I have created...
================================================================

|+-------+---------------------+------------------------------------------------------------+
| LogId | LogDate | LogMessage |
+-------+---------------------+------------------------------------------------------------+
| 37 | 2006-05-03 15:39:28 | /new_userpage_process.php initialized. Creating buffer... | | 38 | 2006-05-03 15:39:28 | Connecting to OLS Server... | | 39 | 2006-05-03 15:39:28 | Connected to (server):(port). Sending XML request... | | 40 | 2006-05-03 15:39:28 | XML request sent. Waiting for XML response... | | 41 | 2006-05-03 15:40:34 | XML response received. | | 42 | 2006-05-03 15:40:34 | Buffering complete. Redirecting to thanks.php?actiontype=2 |
+-------+---------------------+------------------------------------------------------------+

 ================================================================

As you can see, there is a lapse of more than one minutes between LogId's 40 and 41, which is very slow on a production environment. The script above acts as a client to another application server listening on a remote port, running on Java. What the remote application reports is that it receives the connection request, receives the request data, and transmits the response data at the same second, while my application receives the response one minute after the request was sent!

Other implementations in the enterprise using different architectures and technologies/languages do not experience the same issue as my app does.

Please help. Comments, suggestions and thoughts through the group or private mail are deeply appreciated.

Thanks!
--
Oliver John V. Tibi
Software Programmer/Web Application Developer
IAMD Software Solutions
[EMAIL PROTECTED]
"Live free() or die()."

--- End Message ---

Reply via email to