Jump to content

OOP why does this not work?


johnsmith153

Recommended Posts

Why does this not work (of course if I change the line I have commented then it will)?

 

I am trying to see how I would do something like this.

 


<?php

class ClassName {

var $var = "hello";

public function methodName($class, $method) {
	$class::$method($this->var);
	//Class2::anotherMethod($this->var);	
}

}

class Class2 {

static function anotherMethod($val) {
	echo "You said: ".$val;
}

}

$newClass = new ClassName();
$newClass->methodName("Class2", "anotherMethod");

?>

Link to comment
https://forums.phpfreaks.com/topic/207731-oop-why-does-this-not-work/
Share on other sites

what version of php are you using when i run it on 5.3 it works but if i would have to guess i would say your on an older version of php because if you look here

 

http://php.net/manual/en/language.oop5.static.php

 

it says that the first method of calling a static function was added in 5.3

what version of php are you using when i run it on 5.3 it works but if i would have to guess i would say your on an older version of php because if you look here

 

http://php.net/manual/en/language.oop5.static.php

 

it says that the first method of calling a static function was added in 5.3

 

Yes, use this:

 

call_user_func(array($class, $method), $this->var);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.