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 > new $foo->className(); Class name must be a valid object or a string
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
new $foo->className(); Class name must be a valid object or a string

Réponse
 
LinkBack Outils de la discussion
Vieux 04/05/2008, 01h46   #1
Jack Bates
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut new $foo->className(); Class name must be a valid object or a string

I am trying to load PHP objects stored in a database, where the class
name is stored in a column:

$object = new $resultSet->getString(1);

This fails for the same reason that the following fails:

<?php

class Foo
{
public function className()
{
return 'Foo';
}
}

$foo = new Foo;
$bar = new $foo->className();

Fatal error: Class name must be a valid object or a string in test.php
on line 12

I guess this error is due to the confusion of parsing "()" as the
argument list for the "className" function, or the "Foo" constructor...

I work around this error by using a temp variable:

$tmp = $foo->className(); $bar = new $tmp;

- however the above reads like hacky code : (

When calling dynamically named functions, I generally use
call_user_func() to avoid awkwardness with $object->$tmp($arg1, ...)

In other words, I prefer:

call_user_func(array($object, 'get'.$someName), $arg1, ...);

- to:

$tmp = 'get'.$someName; $object->$tmp($arg1, ...);

However there does not appear to be an analog of call_user_func() for
constructing new instances of dynamically named classes?

If I recall correctly, there was also a way to work around calling
dynamically named functions (e.g. $object->$tmp($arg1, ...) using
curly braces:

$object->{'get'.$someName}($arg1, ...);

- however I cannot recall the exact syntax.

Can anyone confirm that there is a curly brace syntax for calling
dynamically named functions? Could it be applied to instantiating
dynamically named classes?

Can anyone recommend a cleaner alternative to:

$tmp = $foo->className(); $bar = new $tmp;

Thanks and best wishes, Jack

  Réponse avec citation
Vieux 04/05/2008, 02h31   #2
Stut
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] new $foo->className(); Class name must be a valid object or a string

On 4 May 2008, at 00:46, Jack Bates wrote:
> I am trying to load PHP objects stored in a database, where the class
> name is stored in a column:
>
> $object = new $resultSet->getString(1);
>
> This fails for the same reason that the following fails:
>
> <?php
>
> class Foo
> {
> public function className()
> {
> return 'Foo';
> }
> }
>
> $foo = new Foo;
> $bar = new $foo->className();


I would rather have a factory method that returns a new instance of
the class. There's no need for the outside world to know the class name.

<?php
class Foo
{
public function newInstance()
{
return new self();
}

public function test($a)
{
echo 'test: '.$a."\n";
}
}

$foo = new Foo;
$foo->test('foo');
$bar = $foo->newInstance();
$bar->test('bar');
?>

However, if you insist on doing it your way can I make a small
suggestion? It's better to spend your time on functionality rather
than finding ways to save some typing. I see no reason to try to
combine the two statements - saving typing and a pitiful amount of
disk space are the only benefits.

-Stut

--
http://stut.net/
  Réponse avec citation
Vieux 04/05/2008, 02h59   #3
Casey
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] new $foo->className(); Class name must be a valid object or a string


On May 3, 2008, at 4:46 PM, Jack Bates <ms419@freezone.co.uk> wrote:

> I am trying to load PHP objects stored in a database, where the class
> name is stored in a column:
>
> $object = new $resultSet->getString(1);
>
> This fails for the same reason that the following fails:
>
> <?php
>
> class Foo
> {
> public function className()
> {
> return 'Foo';
> }
> }
>
> $foo = new Foo;
> $bar = new $foo->className();
>
> Fatal error: Class name must be a valid object or a string in test.php
> on line 12
>
> I guess this error is due to the confusion of parsing "()" as the
> argument list for the "className" function, or the "Foo"
> constructor...
>
> I work around this error by using a temp variable:
>
> $tmp = $foo->className(); $bar = new $tmp;
>
> - however the above reads like hacky code : (
>
> When calling dynamically named functions, I generally use
> call_user_func() to avoid awkwardness with $object->$tmp($arg1, ...)
>
> In other words, I prefer:
>
> call_user_func(array($object, 'get'.$someName), $arg1, ...);
>
> - to:
>
> $tmp = 'get'.$someName; $object->$tmp($arg1, ...);
>
> However there does not appear to be an analog of call_user_func() for
> constructing new instances of dynamically named classes?
>
> If I recall correctly, there was also a way to work around calling
> dynamically named functions (e.g. $object->$tmp($arg1, ...) using
> curly braces:
>
> $object->{'get'.$someName}($arg1, ...);
>
> - however I cannot recall the exact syntax.
>
> Can anyone confirm that there is a curly brace syntax for calling
> dynamically named functions? Could it be applied to instantiating
> dynamically named classes?
>
> Can anyone recommend a cleaner alternative to:
>
> $tmp = $foo->className(); $bar = new $tmp;
>
> Thanks and best wishes, Jack
>

Does...
<?php
$bar = new $foo->className()();
?>
....work?

Otherwise, I'd just do...
<?php
$className = $foo->className();
$bar = new $className;
?>
....instead of $tmp.
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.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 20h32.


É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,09903 seconds with 11 queries