Reply to Re: Call to Static Method PHP5

Your name:

Reply:


Posted by "M. Sokolewicz" on 10/20/18 11:24

how about using:
return call_user_func(array($object1, 'do_static'));

I've also modified your code so it works with both an object and a class:
<?php
class Class1 {
static function do_static() {
echo 'ok';
}
}

function use_static($object1) {
if(is_object($object1)) {
$class = get_class($object1);
} else {
$class = $object1;
}
return call_user_func(array($class, 'do_static'));
}

use_static('Class1');
$obj = new Class1;
use_static($obj);
?>


Adrian Cid Almaguer wrote:
> Hi:
>
> I had the fallowing trouble while using a static method call on php5. Here's
> my solution, i will really apreciate if anyone else can find another way
> around..
>
> The problem:
>
> I have a fuction wich get a parameter that's an objetc, and that objetc
> belongs to a class wich has a static method ( several class's may have the
> same method, that's the reason for i never know for sure wich class the
> objetc belongs to...) it has to be call inside the fuction... the solution i
> found was:
>
> <?
> class Class1 {
> static function do_static() { echo 'ok'; }
> }
>
> function use_static($object1) {
> $class1 = get_class($object1);
> $method = new ReflectionMethod($class1, 'do_static');
> $method->invoke(null);
> // First, I thinks do that
> // $class1 = get_class($object1);
> // $class1::do_static;
> // But, I recieve an error
>
> }
>
> $object1 = new Class1;
> use_static($object1);
>
> ?>
>
> gretings
>
> Adrian
>

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация