Discussion:
Building variable getter-method
Michael Bakonyi
2014-05-29 14:06:53 UTC
Permalink
Hi,

I'd like to do something like this:

if (method_exists($object, 'get' . ucfirst($property))) {
$children = $object->{'get' . ucfirst($property)};
}

but this is not working unfortunately – $children is always NULL
although the getter exits. Is there a another way to build a dynamic getter?

Cheers,
Michael
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Tsvetan Nikolov
2014-05-29 15:13:12 UTC
Permalink
make sure your getter class returns something!
Post by Michael Bakonyi
Hi,
if (method_exists($object, 'get' . ucfirst($property))) {
$children = $object->{'get' . ucfirst($property)};
}
but this is not working unfortunately – $children is always NULL although
the getter exits. Is there a another way to build a dynamic getter?
Cheers,
Michael
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jim Lucas
2014-05-29 15:14:48 UTC
Permalink
Post by Michael Bakonyi
Hi,
if (method_exists($object, 'get' . ucfirst($property))) {
Untested, but I do recall having to build a temp variable first, then use it
in my call. Also, if you are trying to call a method, you forgot the '()'. So...

$method = 'get' . ucfirst($property);
$children = $object->$method();
Post by Michael Bakonyi
$children = $object->{'get' . ucfirst($property)};
}
but this is not working unfortunately – $children is always NULL although the
getter exits. Is there a another way to build a dynamic getter?
Cheers,
Michael
--
Jim Lucas

http://www.cmsws.com/
http://www.cmsws.com/examples/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jim Lucas
2014-05-29 15:22:31 UTC
Permalink
Post by Jim Lucas
Post by Michael Bakonyi
Hi,
if (method_exists($object, 'get' . ucfirst($property))) {
Untested, but I do recall having to build a temp variable first, then use it
in my call. Also, if you are trying to call a method, you forgot the '()'.
So...
$method = 'get' . ucfirst($property);
$children = $object->$method();
Post by Michael Bakonyi
$children = $object->{'get' . ucfirst($property)};
}
Better yet, rearrange things a little.

$method = 'get' . ucfirst($property);

if ( method_exists($object, $method) ) {
$children = $object->$method();
}
Post by Jim Lucas
Post by Michael Bakonyi
but this is not working unfortunately – $children is always NULL although the
getter exits. Is there a another way to build a dynamic getter?
Cheers,
Michael
--
Jim Lucas

http://www.cmsws.com/
http://www.cmsws.com/examples/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Michael Bakonyi
2014-06-02 09:12:05 UTC
Permalink
Great, thx a lot for your help! :)

Cheers,
Michael

--
Michael Bakonyi
Alstater Str. 55
69124 Heidelberg

Tel: 0170 40 232 62
Skype: m.bakonyi
Post by Jim Lucas
Post by Jim Lucas
Post by Michael Bakonyi
Hi,
if (method_exists($object, 'get' . ucfirst($property))) {
Untested, but I do recall having to build a temp variable first, then use it
in my call. Also, if you are trying to call a method, you forgot the '()'.
So...
$method = 'get' . ucfirst($property);
$children = $object->$method();
Post by Michael Bakonyi
$children = $object->{'get' . ucfirst($property)};
}
Better yet, rearrange things a little.
$method = 'get' . ucfirst($property);
if ( method_exists($object, $method) ) {
$children = $object->$method();
}
Post by Jim Lucas
Post by Michael Bakonyi
but this is not working unfortunately – $children is always NULL although the
getter exits. Is there a another way to build a dynamic getter?
Cheers,
Michael
--
Jim Lucas
http://www.cmsws.com/
http://www.cmsws.com/examples/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Stuart Dallas
2014-05-29 15:20:22 UTC
Permalink
Post by Michael Bakonyi
if (method_exists($object, 'get' . ucfirst($property))) {
$children = $object->{'get' . ucfirst($property)};
The line above is not calling a method, it’s accessing a member variable. The following might work (note the added brackets at the end), but if not you’ll need to put the name of the method in to a temporary variable as Jim suggests.

$children = $object->{'get' . ucfirst($property)}();
Post by Michael Bakonyi
}
but this is not working unfortunately – $children is always NULL although the getter exits. Is there a another way to build a dynamic getter?
-Stuart
--
Stuart Dallas
3ft9 Ltd
http://3ft9.com/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Larry Garfield
2014-05-29 17:11:09 UTC
Permalink
Post by Michael Bakonyi
Hi,
if (method_exists($object, 'get' . ucfirst($property))) {
$children = $object->{'get' . ucfirst($property)};
}
but this is not working unfortunately – $children is always NULL
although the getter exits. Is there a another way to build a dynamic getter?
Cheers,
Michael
Side note: PHP is, technically, case-insensitive for
class/function/method names. So you don't need the ucfirst() calls.
Calling $object->foo() and $object->Foo() do the same thing.

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