PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > Array pointer to a function
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Array pointer to a function

Réponse
 
LinkBack Outils de la discussion
Vieux 08/04/2008, 05h00   #1
hce
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Array pointer to a function

Hi,

Is it possible for an array to point a function:

Asignmanet

$ArrayPointer = $this->MyFunction;

Invoke:

$ArraryPointer();
  Réponse avec citation
Vieux 08/04/2008, 05h55   #2
Nathan Nobbe
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Array pointer to a function

On Tue, Apr 8, 2008 at 12:00 AM, hce <webmail.hce@gmail.com> wrote:

> Hi,
>
> Is it possible for an array to point a function:
>
> Asignmanet
>
> $ArrayPointer = $this->MyFunction;
>
> Invoke:
>
> $ArraryPointer();



i would recommend you investigate variable functions

1. http://www.php.net/manual/en/functio...-functions.php

so you dont exactly get functional language behavior; but you can 'pass
around a function'. so for example; if you have
<?php
function someFunc() {}
/**
* then you can pass around strings that refer to it; as in
*/
$someFuncPointer = 'someFunc';

/**
* then you can call it using the variable function construct; as in
*/
$someFuncPointer(); // invoking someFunc()
?>

you can pass parameters to such an invocation; as in
<?php
$someFuncPointer(1, $b, $yaddaYadda);
?>

and it works for object instances
<?php
class MyClass { function doStuff() {} }
$b = new MyClass();
$b->doStuff();
?>

and it works for static object method calls
<?php
class OtherClass { static function doMoreStuff() {} }
$b = 'doMoreStuff';
?>

you can also store the class name or instance in variables and it still
works. here the output of a little php -a session

<?php
class M { static function b() {} function n() {} }
$m = 'M';
$b = 'b';
$m::$b();
$n = new M();
$n->b();
$n->$b();
?>

and thats just the tip of the iceberg! there is also the php psuedo type
callback; and there are some other functions that are pertinent.
call_user_func(); // very similar to variable functions
call_user_func_array()

-nathan

  Réponse avec citation
Vieux 08/04/2008, 05h58   #3
Robert Cummings
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Array pointer to a function


On Tue, 2008-04-08 at 14:00 +1000, hce wrote:
> Hi,
>
> Is it possible for an array to point a function:
>
> Asignmanet
>
> $ArrayPointer = $this->MyFunction;
>
> Invoke:
>
> $ArraryPointer();


No, you can't do what you've done above. What you can do is...

<?php

$ptr = array( 'obj' => &$this, 'method' => 'MyFunction' );
$ptr['obj']->$ptr['method']();

?>

Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 00h29.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10546 seconds with 11 queries