How to redefine a function if it doesn"t exist?

How to redefine a function if it doesn"t exist?

am 30.03.2010 15:16:57 von Andre Polykanine

Hello everyone,
Sorry, I've forgotten how to do this...
I need a quoted_printable_encode function but it's available only
since PHP 5.3. How do I redefine that function only if PHP version is
lower than 5.3?
Would it be valid:
function quoted_printable_encode ($str) {
$x=quoted_printable_encode ($str);
if (!isset($x)) {
// blah blah, alternative code
} else {
return $x;
}
}
Is it valid code or not?)
Thanks!


--
With best regards from Ukraine,
Andre
Http://oire.org/ - The Fantasy blogs of Oire
Skype: Francophile; Wlm&MSN: arthaelon @ yandex.ru; Jabber: arthaelon @ jabber.org
Yahoo! messenger: andre.polykanine; ICQ: 191749952
Twitter: http://twitter.com/m_elensule


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

Re: How to redefine a function if it doesn"t exist?

am 30.03.2010 15:20:09 von Ashley Sheridan

--=-/QALrB80aR2X6ZuCtEIt
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

On Tue, 2010-03-30 at 16:16 +0300, Andre Polykanine wrote:

> Hello everyone,
> Sorry, I've forgotten how to do this...
> I need a quoted_printable_encode function but it's available only
> since PHP 5.3. How do I redefine that function only if PHP version is
> lower than 5.3?
> Would it be valid:
> function quoted_printable_encode ($str) {
> $x=quoted_printable_encode ($str);
> if (!isset($x)) {
> // blah blah, alternative code
> } else {
> return $x;
> }
> }
> Is it valid code or not?)
> Thanks!
>
>
> --
> With best regards from Ukraine,
> Andre
> Http://oire.org/ - The Fantasy blogs of Oire
> Skype: Francophile; Wlm&MSN: arthaelon @ yandex.ru; Jabber: arthaelon @ jabber.org
> Yahoo! messenger: andre.polykanine; ICQ: 191749952
> Twitter: http://twitter.com/m_elensule
>
>


The custom way is to use function_exists() to check to see if the
function exists:

if (!function_exists('quoted_printable_encode'))
{
function quoted_printable_encode()
{
your code to replicate the functionality here
}
}

Then, you can call quoted_printable_encode safe in the knowledge that
there will always be a definition for it.


Thanks,
Ash
http://www.ashleysheridan.co.uk



--=-/QALrB80aR2X6ZuCtEIt--

Re: How to redefine a function if it doesn"t exist?

am 30.03.2010 15:23:34 von David Otton

On 30 March 2010 14:16, Andre Polykanine wrote:

> I need a quoted_printable_encode function but it's available only
> since PHP 5.3. How do I redefine that function only if PHP version is
> lower than 5.3?

function_exists().

if (!function_exists('myfunc')) {
function myfunc() {
;
}
}

http://uk3.php.net/function_exists

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

Re: How to redefine a function if it doesn"t exist?

am 30.03.2010 17:14:35 von Richard Quadling

On 30 March 2010 14:20, Ashley Sheridan wrote:
> On Tue, 2010-03-30 at 16:16 +0300, Andre Polykanine wrote:
>
>> Hello everyone,
>> Sorry, I've forgotten how to do this...
>> I need a quoted_printable_encode function but it's available only
>> since PHP 5.3. How do I redefine that function only if PHP version is
>> lower than 5.3?
>> Would it be valid:
>> function quoted_printable_encode ($str) {
>> $x=3Dquoted_printable_encode ($str);
>> if (!isset($x)) {
>> // blah blah, alternative code
>> } else {
>> return $x;
>> }
>> }
>> Is it valid code or not?)
>>                     =
     Thanks!
>>
>>
>> --
>> With best regards from Ukraine,
>> Andre
>> Http://oire.org/ - The Fantasy blogs of Oire
>> Skype: Francophile; Wlm&MSN: arthaelon @ yandex.ru; Jabber: arthaelon @ =
jabber.org
>> Yahoo! messenger: andre.polykanine; ICQ: 191749952
>> Twitter: http://twitter.com/m_elensule
>>
>>
>
>
> The custom way is to use function_exists() to check to see if the
> function exists:
>
> if (!function_exists('quoted_printable_encode'))
> {
>    function quoted_printable_encode()
>    {
>        your code to replicate the functionality here
>    }
> }
>
> Then, you can call quoted_printable_encode safe in the knowledge that
> there will always be a definition for it.
>
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
>
>

http://pear.php.net/package/PHP_Compat contains a LOT of userland
functions to allow for forward compatibility of old code.

"PHP_Compat provides drop-in functions and constants for compatibility
with newer versions of PHP, environment emulation, and an API to allow
for version independent authoring."



--=20
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=3DZEND002498&r=3D213474=
731
ZOPA : http://uk.zopa.com/member/RQuadling

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

Re: How to redefine a function if it doesn"t exist?

am 30.03.2010 17:26:42 von Igor Escobar

--001636e0b9b5b76baa04830641df
Content-Type: text/plain; charset=ISO-8859-1

See
http://br2.php.net/manual/en/function.create-function.php


Regards,
Igor Escobar
Systems Analyst & Interface Designer

+ http://blog.igorescobar.com
+ http://www.igorescobar.com
+ @igorescobar (twitter)





On Tue, Mar 30, 2010 at 10:23 AM, David Otton <
phpmail@jawbone.freeserve.co.uk> wrote:

> On 30 March 2010 14:16, Andre Polykanine wrote:
>
> > I need a quoted_printable_encode function but it's available only
> > since PHP 5.3. How do I redefine that function only if PHP version is
> > lower than 5.3?
>
> function_exists().
>
> if (!function_exists('myfunc')) {
> function myfunc() {
> ;
> }
> }
>
> http://uk3.php.net/function_exists
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--001636e0b9b5b76baa04830641df--

Re: How to redefine a function if it doesn"t exist?

am 30.03.2010 17:28:23 von Ashley Sheridan

--=-RqaYJKnFDGaSNLcQXBVm
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

On Tue, 2010-03-30 at 12:26 -0300, Igor Escobar wrote:

> See
> http://br2.php.net/manual/en/function.create-function.php
>
>
> Regards,
> Igor Escobar
> Systems Analyst & Interface Designer
>
> + http://blog.igorescobar.com
> + http://www.igorescobar.com
> + @igorescobar (twitter)
>
>
>
>
>
> On Tue, Mar 30, 2010 at 10:23 AM, David Otton <
> phpmail@jawbone.freeserve.co.uk> wrote:
>
> > On 30 March 2010 14:16, Andre Polykanine wrote:
> >
> > > I need a quoted_printable_encode function but it's available only
> > > since PHP 5.3. How do I redefine that function only if PHP version is
> > > lower than 5.3?
> >
> > function_exists().
> >
> > if (!function_exists('myfunc')) {
> > function myfunc() {
> > ;
> > }
> > }
> >
> > http://uk3.php.net/function_exists
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >


That is only really useful for creating a function at run-time, not for
defining a function if it doesn't exist. As I and several others have
pointed out, the best way is to use function_exists() to determine
whether to create one or not. Presumably if you need to redefine a
function, you know its arguments before the code executes.

Thanks,
Ash
http://www.ashleysheridan.co.uk



--=-RqaYJKnFDGaSNLcQXBVm--