I want to call a private class method from trait. If I do $this->privateMethod();
the call is made.
Since I want to only call that method if exists, I've implemented a isCallable()
function; but this function is returning false
when I was expecting true
.
I'm missing something about scopes, why is this happening and how can I get is_callable()
to return true?
See this example code:
<?php
function isCallable($obj, $method)
{
//$test1 = method_exists($obj, $method);
//var_dump($test1);
//TRUE
//$test2 = is_callable([$obj, $method]);
//var_dump($test2);
//FALSE
return (method_exists($obj, $method) && is_callable([$obj, $method]));
}
trait helper
{
public function show()
{
echo "show!";
if(isCallable($this, "afterShow"))
{
$this->afterShow();
}
//$this->afterShow();
}
}
class my_class
{
use helper;
private function afterShow()
{
echo "...then this.";
}
}
$objMyClass = new my_class();
$objMyClass->show();