php-general Digest 14 Mar 2011 20:31:21 -0000 Issue 7226

Topics (messages 311826 through 311836):

Handling exit in eval
        311826 by: Thomas Björk
        311827 by: Thomas Björk
        311828 by: Richard Quadling
        311829 by: Richard Quadling
        311830 by: Thomas Björk
        311834 by: Richard Quadling

assistance with php not running properly
        311831 by: Jack

PHP Fatal error:  Cannot redeclare class
        311832 by: Brendan_Crowley.DellTeam.com
        311833 by: Daniel Brown
        311835 by: Brendan_Crowley.DellTeam.com

Deleting elements from the middle of an array
        311836 by: Paul M Foster

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
Is there any way to emulate an exit in eval without exiting the calling
script.

<?php
$s = 'echo "Shows"; exit; echo "Doesn\'t show"; ';
eval($s);
echo "Never comes here";
?>


I would like to do something like this:
<?php
$s = 'echo "Shows"; exit; echo "Doesn\'t show"; ';

function MakeItStop() {
  // Do something to make the eval stop
  // without halting the script
}

$s = str_replace('exit;', 'MakeItStop();', $s);

eval($s);
echo "Never comes here";
?>


A simple return would fix it IF the return isn't located within a
function.


Any good suggestions?

Thanks in advanced
Thomas Björk


--- End Message ---
--- Begin Message ---
Is there any way to emulate an exit in eval without exiting the calling
script.

<?php
$s = 'echo "Shows"; exit; echo "Doesn\'t show"; ';
eval($s);
echo "Never comes here";
?>


I would like to do something like this:
<?php
$s = 'echo "Shows"; exit; echo "Doesn\'t show"; ';

function MakeItStop() {
  // Do something to make the eval stop
  // without halting the script
}

$s = str_replace('exit;', 'MakeItStop();', $s);

eval($s);
echo "Never comes here";
?>


A simple return would fix it IF the return isn't located within a
function.


Any good suggestions?

Thanks in advanced
Thomas Björk

--- End Message ---
--- Begin Message ---
2011/3/14 Thomas Björk <t...@bytecode.se>:
> Is there any way to emulate an exit in eval without exiting the calling
> script.
>
> <?php
> $s = 'echo "Shows"; exit; echo "Doesn\'t show"; ';
> eval($s);
> echo "Never comes here";
> ?>
>
>
> I would like to do something like this:
> <?php
> $s = 'echo "Shows"; exit; echo "Doesn\'t show"; ';
>
> function MakeItStop() {
>  // Do something to make the eval stop
>  // without halting the script
> }
>
> $s = str_replace('exit;', 'MakeItStop();', $s);
>
> eval($s);
> echo "Never comes here";
> ?>
>
>
> A simple return would fix it IF the return isn't located within a
> function.
>
>
> Any good suggestions?
>
> Thanks in advanced
> Thomas Björk
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

Can you provide a more realistic example?

Just don't code anything after the last line of code that contains the
exit? Surely.

http://uk.php.net/manual/en/function.eval.php does say that ...

"A return statement will immediately terminate the evaluation of the string ."

So, there's your answer I suppose.

Replace "exit" with "return".

-- 
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY

--- End Message ---
--- Begin Message ---
2011/3/14 Richard Quadling <rquadl...@gmail.com>:

And where is my UNDO button.

Essentially, having exit in the eval code is no different to having it
in non-eval code. eval isn't a separate entity which can be
terminated.

So, code the eval code differently.

If you can provide some examples, maybe we can come up with a better solution.



-- 
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY

--- End Message ---
--- Begin Message ---
Richard Quadling wrote:

> 2011/3/14 Richard Quadling <rquadl...@gmail.com>:
>
> And where is my UNDO button.
>
> Essentially, having exit in the eval code is no different to having it
> in non-eval code. eval isn't a separate entity which can be
> terminated.
>
> So, code the eval code differently.
>
> If you can provide some examples, maybe we can come up with a better solution.
>
>
>

I'm developing a kind of sandbox where you will be able to run a script
without affecting your current context. The script is loaded and parsed
which gives me the opportunity to allow or deny functions and classes
used in the script.
The only problem I have so far is the exit function which, it if is
allowed, terminates the calling script as well as the eval'ed script.

By using a return the problem is solved as long as the user doesn't
write a function containing an exit.

Here is a simple script thet the user might write (probably as useful as
a "Hello, world!".

<?php
function DoSomethingOrExit($test) {
  if($test == 1) {
    exit;
  }
  return;
}

DoSomethingOrExit(0);
echo "Hello";
DoSomethingOrExit(1);
echo ", world!";
exit;
echo "Bye";
?>

By replacing exit with return this code will echo "Hello, world!" before
returning without echoing "Bye".


Another perfect solution would be if there was a way to trigger an error
within the eval'ed script that "crashes" it and returns to the calling
script. But then it just comes back to the entity-problem as you pointed
out.


/Thomas

--- End Message ---
--- Begin Message ---
On 14 March 2011 12:14, DELiTH <t...@bytecode.se> wrote:
> Richard Quadling wrote:
>
>> 2011/3/14 Richard Quadling <rquadl...@gmail.com>:
>>
>> And where is my UNDO button.
>>
>> Essentially, having exit in the eval code is no different to having it
>> in non-eval code. eval isn't a separate entity which can be
>> terminated.
>>
>> So, code the eval code differently.
>>
>> If you can provide some examples, maybe we can come up with a better 
>> solution.
>>
>>
>>
>
> I'm developing a kind of sandbox where you will be able to run a script
> without affecting your current context. The script is loaded and parsed
> which gives me the opportunity to allow or deny functions and classes
> used in the script.
> The only problem I have so far is the exit function which, it if is
> allowed, terminates the calling script as well as the eval'ed script.
>
> By using a return the problem is solved as long as the user doesn't
> write a function containing an exit.
>
> Here is a simple script thet the user might write (probably as useful as
> a "Hello, world!".
>
> <?php
> function DoSomethingOrExit($test) {
>  if($test == 1) {
>    exit;
>  }
>  return;
> }
>
> DoSomethingOrExit(0);
> echo "Hello";
> DoSomethingOrExit(1);
> echo ", world!";
> exit;
> echo "Bye";
> ?>
>
> By replacing exit with return this code will echo "Hello, world!" before
> returning without echoing "Bye".
>
>
> Another perfect solution would be if there was a way to trigger an error
> within the eval'ed script that "crashes" it and returns to the calling
> script. But then it just comes back to the entity-problem as you pointed
> out.
>
>
> /Thomas
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

Why not just invoke a separate instance of PHP using exec() ?

Or take a look at runkit/runkit_Sandbox :
http://uk.php.net/manual/en/book.runkit.php

-- 
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY

--- End Message ---
--- Begin Message ---
Hello All,

 

I have a problem where we tried to install mod_security for apache, failed
and backed out of the install.

Were not sure if that's what did it, but when we run certain php scripts
they are no longer working, giving us blank screen ( web browser screen ) no
errors in http error log or php error log.

 

I would like to hire someone to fix this ASAP, but not sure what companies
out there are good apache/php server support guys.  This is a SERVER level
issue, not a programming issue.


Any suggestions appreciated.

 

Thanks!

Jack

 


--- End Message ---
--- Begin Message ---
Hi, I get the following error in my apache error log:
[14-Mar-2011 14:17:27] PHP Fatal error:  Cannot redeclare class
I created a simplified set of php files and classes to formulate this question.
I have 5 php files; 2 of which declare a class that has the same name in both. 
I know it would be very easy to change one of the class names, but I'm 
restricted because I'm working with legacy working code and an interface 
definition that cannot change either, here are the php files, how can I resolve 
this?, any help appreciated.
Thanks,
Brendan.

## phpFileOne.php ##
<?php
include_once './phpFileTwo.php';
include_once './phpFileFour.php';

$zIns = new Z();
$result = a($zIns);

// class Z is defined in phpFileFour.php
function a(Z $dataX) {
      $dataZ;
      $dataY = b($dataZ); // b is defined in phpFileTwo.php
      return $dataY;
}

echo '<p>result: '.$result.'</p>';
?>

## phpFileTwo.php ##
<?php
include_once './phpFileThree.php';
function b($dataW) {
      $dataU;
      $dataV = new A($dataU); // class A is defined in phpFileThree.php
      return $dataV->c();
}
?>

## phpFileThree.php ##
<?php
include_once './phpFileFive.php';
// class Z is defined in phpFileFive.php
class A extends Z {
      function __construct($dataM) {

      }

      function c() {
            return 'cValue';
      }
}
?>

## phpFileFour.php ##
<?php
// class Z defined in interface definition - cannot change name
class Z {
}
?>

## phpFileFive.php ##
<?php
// this class and class Z in phpFileFour.php have the same name!
// Legacy class Z - cannot change name
class Z {
}
?>






--- End Message ---
--- Begin Message ---
On Mon, Mar 14, 2011 at 10:34,  <brendan_crow...@dellteam.com> wrote:
> Hi, I get the following error in my apache error log:
> [14-Mar-2011 14:17:27] PHP Fatal error:  Cannot redeclare class
> I created a simplified set of php files and classes to formulate this 
> question.
> I have 5 php files; 2 of which declare a class that has the same name in 
> both. I know it would be very easy to change one of the class names, but I'm 
> restricted because I'm working with legacy working code and an interface 
> definition that cannot change either, here are the php files, how can I 
> resolve this?, any help appreciated.

    If it's legacy code and this is how it was, that means it never
worked properly, because you can't redeclare classes or functions by
the same name.  That said, if you're restricted from changing the
class names, how about wrapping the classes with a !class_exists()
call?

        http://php.net/class_exists


> ## phpFileOne.php ##
> <?php
> include_once './phpFileTwo.php';
> include_once './phpFileFour.php';
>
> $zIns = new Z();
> $result = a($zIns);
>
> // class Z is defined in phpFileFour.php
> function a(Z $dataX) {
>      $dataZ;
>      $dataY = b($dataZ); // b is defined in phpFileTwo.php
>      return $dataY;
> }
>
> echo '<p>result: '.$result.'</p>';
> ?>
>
> ## phpFileTwo.php ##
> <?php
> include_once './phpFileThree.php';
> function b($dataW) {
>      $dataU;
>      $dataV = new A($dataU); // class A is defined in phpFileThree.php
>      return $dataV->c();
> }
> ?>
>
> ## phpFileThree.php ##
> <?php
> include_once './phpFileFive.php';
> // class Z is defined in phpFileFive.php
> class A extends Z {
>      function __construct($dataM) {
>
>      }
>
>      function c() {
>            return 'cValue';
>      }
> }
> ?>
>
> ## phpFileFour.php ##
> <?php
> // class Z defined in interface definition - cannot change name
> class Z {
> }
> ?>
>
> ## phpFileFive.php ##
> <?php
> // this class and class Z in phpFileFour.php have the same name!
> // Legacy class Z - cannot change name
> class Z {
> }
> ?>
>
>
>
>
>
>



-- 
</Daniel P. Brown>
Network Infrastructure Manager
http://www.php.net/

--- End Message ---
--- Begin Message ---
Thanks Louis,

Worked a treat!

-----Original Message-----
From: Louis Huppenbauer [mailto:louis.huppenba...@gmail.com] 
Sent: 14 March 2011 15:10
To: Crowley, Brendan - Dell Team
Subject: Re: [PHP] PHP Fatal error: Cannot redeclare class

what about http://ch2.php.net/namespaces in php 5.3?

2011/3/14  <brendan_crow...@dellteam.com>:
> Hi, I get the following error in my apache error log:
> [14-Mar-2011 14:17:27] PHP Fatal error:  Cannot redeclare class I 
> created a simplified set of php files and classes to formulate this question.
> I have 5 php files; 2 of which declare a class that has the same name in 
> both. I know it would be very easy to change one of the class names, but I'm 
> restricted because I'm working with legacy working code and an interface 
> definition that cannot change either, here are the php files, how can I 
> resolve this?, any help appreciated.
> Thanks,
> Brendan.
>
> ## phpFileOne.php ##
> <?php
> include_once './phpFileTwo.php';
> include_once './phpFileFour.php';
>
> $zIns = new Z();
> $result = a($zIns);
>
> // class Z is defined in phpFileFour.php function a(Z $dataX) {
>      $dataZ;
>      $dataY = b($dataZ); // b is defined in phpFileTwo.php
>      return $dataY;
> }
>
> echo '<p>result: '.$result.'</p>';
> ?>
>
> ## phpFileTwo.php ##
> <?php
> include_once './phpFileThree.php';
> function b($dataW) {
>      $dataU;
>      $dataV = new A($dataU); // class A is defined in phpFileThree.php
>      return $dataV->c();
> }
> ?>
>
> ## phpFileThree.php ##
> <?php
> include_once './phpFileFive.php';
> // class Z is defined in phpFileFive.php class A extends Z {
>      function __construct($dataM) {
>
>      }
>
>      function c() {
>            return 'cValue';
>      }
> }
> ?>
>
> ## phpFileFour.php ##
> <?php
> // class Z defined in interface definition - cannot change name class 
> Z { } ?>
>
> ## phpFileFive.php ##
> <?php
> // this class and class Z in phpFileFour.php have the same name!
> // Legacy class Z - cannot change name class Z { } ?>
>
>
>
>
>
>

--- End Message ---
--- Begin Message ---
Here's what I need to do: I have an indexed array, from which I need to
delete elements in the middle. Once completed, the indexes should be
numerically in sequence, as they were when I first encountered the
array. That is:

Before:
$arr = array(0 => 5, 1 => 6, 2 => 7, 3 => 8, 4 => 9, 5 => 10);

After:
$arr = array(0 => 5, 1 => 6, 2 => 9, 3 => 10);


I've tried:

1) Using for() with unset(). Elements are deleted, but the indexes are
no longer sequential.

2) Using for() with array_splice(). Understandably, deletes certain
elements but not others.

3) Using foreach() with referenced array elements. Neither unset() nor
array_splice() appear to have any effect on the array at all.

4) while() loop using current() and the like. But these array functions
return values, not references, so the array isn't actually modified.

5) array_walk() with unset() array_splice(). No effect on the array.

Anyone know how to do this, or know of a reference on how to?

Paul

-- 
Paul M. Foster
http://noferblatz.com
http://quillandmouse.com

--- End Message ---

Reply via email to