Skip to content

Core\Reflect\MethodInfo::isPublic

$methodInfo->isPublic(): bool

Tells you whether code outside the class may call a method.

isPublic() returns true for a public method. It returns false for a protected or private method. Those methods are still in the list that Core\Reflect\ClassInfo::methods returns, so you can see that they exist.

When isPublic() returns false and code outside the class passes that name to Core\Reflect\ClassInfo::call, call throws a LogicError. The method does not run.

The examples below show which methods of a class are public, what happens when you call one that is not, and how a small web router finds the pages a controller offers.

Return value

Returns bool. true for a public method, and false for a protected or private method. Those methods are still in the list that Core\Reflect\ClassInfo::methods returns. If isPublic returns false, Core\Reflect\ClassInfo::call from outside the class throws a LogicError.

Examples

Show which methods are public

<?nvs
// Shows which methods of a class are public.
class Account
{
public function balance(): int
{
return 120;
}
protected function fee(): int
{
return 2;
}
private function audit(): string
{
return 'checked';
}
}
// This prints `audit: no`, `balance: yes` and `fee: no`.
foreach (Core\Reflect::forObject(new Account())->methods() as Core\Reflect\MethodInfo $method) {
echo $method->name(), ': ', $method->isPublic() ? 'yes' : 'no', "\n";
}
Output
audit: no
balance: yes
fee: no

Calling a method that is not public

<?nvs
// Shows what happens when code outside a class calls a method that is not public.
class Account
{
public function balance(): int
{
return 120;
}
private function resetBalance(): int
{
return 0;
}
}
var $account = new Account();
var $info = Core\Reflect::forObject($account);
// `balance` is public, so `call` runs it. This prints `120`.
echo $info->call($account, 'balance', []) as int, "\n";
// `resetBalance` is private. `call` throws a `LogicError` and the method does not run.
try {
$info->call($account, 'resetBalance', []);
} catch (LogicError $error) {
echo 'Not allowed: ', $error->message, "\n";
}
Output
120
Not allowed: `Account::resetBalance` is not public, and a `mixed` receiver is outside every class

Find the pages of a controller

<?nvs
// Shows how a small web router turns the public methods of a controller into pages.
class ShopController
{
public function home(): string
{
return 'Welcome to the shop';
}
public function cart(): string
{
return 'Your cart is empty';
}
private function loadPrices(): string
{
return 'prices';
}
}
class Router
{
public static function pages(Core\Reflect\ClassInfo $info): array<string>
{
array<string> $pages = [];
foreach ($info->methods() as Core\Reflect\MethodInfo $method) {
// A private helper is not a page, so nobody can open `/loadPrices`.
if ($method->isPublic()) {
$pages[] = '/' . $method->name();
}
}
return $pages;
}
}
// This prints `/cart` and `/home`.
foreach (Router::pages(Core\Reflect::forObject(new ShopController())) as string $page) {
echo $page, "\n";
}
Output
/cart
/home