r/PHPhelp • u/Fine-Check7393 • Aug 04 '25
Converting to PHP 8.4
Hi, I have been looking on various sites and trying different things, but i'm a bit stuck.
<?php
function counter1()
{
static $c = 0;
return ++$c;
}
$ref = new ReflectionFunction('counter1');
?>Works absolutely fine, however my code is in a class, so I did the following:
<?php
class Test {
function counter1()
{
static $c = 0;
return ++$c;
}
$ref = new ReflectionFunction('counter1');
}
?>All my logs say is: Error: Function counter1() does not exist,
I've tried using Test::counter1, $this->counter1, SELF::counter1, anything I could think of, but it's not having any of it, my old PHP (7.4 i think) worked fine, so any thoughts / assistance greatly appreciated!
TIA!
8
Upvotes
10
u/LostInCyberSpace-404 Aug 04 '25
You have multiple issues.l, but the error is correct that the function doesn't exist.
Here is a corrected version.
<?php class Test { function counter1() { static $c = 0; return ++$c; } } $ref = new ReflectionMethod('Test', 'counter1'); echo $ref->getName(); // Output: counter1 ?>
3
u/equilni Aug 05 '25
Basic classes.
https://www.php.net/manual/en/language.oop5.php
u/LostInCyberSpace-404 already gave the corrected code.
But your tries...
Would work if your code looked like:
Would only work within the class. Like so:
Again, within the class
3
u/LordAmras Aug 05 '25 edited Aug 05 '25
the
inside the class doesn't really make sense, and any editor with a PHP linter would tell you that before you even try to run the code.
So, in this case the main question is "what are you trying to do ?" and secondly to try to get a linter in your editor so that you will catch this king of simple grammar errors.
Without context I would translate your code like this, but I would probably not using static and globals anymore if you want to move them to classes.
You are not forced to use classes in php 8.4, the first example would work in php8.4, but if you want to refractor the code without context is hard to have a good idea.
2
u/ColonelMustang90 Aug 05 '25
OOP: create static methods to access static members of the class. Then call using ClassName::method().
1
u/nim_port_na_wak Aug 05 '25
Use
new ReflectionClass(Test::class);, then take advantage of your code editor autocomplete function to check which methods are available to achieve the equivalent