Skip to content

My Rant

Mini rants, mostly about Linux admin and PHP code

Month: April 2021

How to call a static method in PHP when you have the class name in a variable

Wrap the variable (that holds the class name) in brackets:

<?php

class A
{
    public static function foo($arg)
    {
        return 'Argument was "' . $arg . '"' . PHP_EOL;
    }
}

$result = A::foo('triggered via named class');
echo $result;

$className = A::class;
$result = ($className)::foo('trigged via variable');
echo $result;

The output of the above code:

php a.php
Argument was "triggered via named class"
Argument was "trigged via variable"
Author iainPosted on 23rd April 2021Categories UncategorisedLeave a comment on How to call a static method in PHP when you have the class name in a variable

Recent Posts

  • Fingerprint authentication in Kubuntu 26.04 (Resolute Raccoon) for the Lenovo ThinkPad T480s
  • Updating TrueNAS Scale when it isn’t installed on the boot drive (HP Proliant Microserver Gen8)
  • Trouble setting up wireguard-ui in docker
  • Use Canon EOS 90D as a webcam for Google Meet / Skype / Zoom / etc on Linux Ubuntu 22.04 with nVidia hardware acceleration
  • SQLSTATE[HY000]: General error: 1366 Incorrect string value: ‘\xE2\x80\x8B\xE2\x80\x8B…’ for column

Recent Comments

  1. stavro purdie on Basic set-up of a 3com 4500 managed network switch
  2. Markus Fornefeld on Basic set-up of a 3com 4500 managed network switch
  3. Alisa on Adding extra fields to FOSUserBundle / SonataUserBundle
  4. Iain Cuthbertson on Setting up an On-Premise instance of Amon
  5. Keith on Setting up an On-Premise instance of Amon
My Rant Proudly powered by WordPress