Page 1 of 1

Does reversi tutorial misuse static methods?

Posted: 16 January 2021, 17:27
by ShaPhi7
When I take game.php from the reversi tutorial and view it in an IDE (I'm using VS Code), I see lots of examples of the following:

$board = self::getBoard();

which get underlined as a warning with the text "Non static method 'getBoard' should not be called statically."

It is only a warning, the code all still works. If I change the code to

$board = $this->getBoard();

the warning goes away, presumably as I am now no longer calling it statically.

Would you regard this as poor code?
If so, what would you generally do instead (static method? call as "$this->xxx")?
If I changed all of the references from self:: to $this, would it make any difference/would anything break for this game on bga?

I'm coming from a Java background, where I'm used to this sort of thing being impossible/a compiler error. I'm new to php, and trying to establish some sort of an idea as to the php coding standards/conventions.

Re: Does reversi tutorial misuse static methods?

Posted: 18 January 2021, 04:41
by Victoria_La
I am not sure wht bga guys use self:: everywhere, I personally use $this->method() in all my games, so can you.

Re: Does reversi tutorial misuse static methods?

Posted: 19 January 2021, 23:46
by Archduke
In a non-static context, $this and self are subtely different, but unless you're making subclasses and redefining methods, they will amount to the same thing.

Against my better judgement, I personally have ended up using "self::" almost everywhere because it's easier to type on my keyboard than "$this->".

Example for the curious:

Code: Select all

<?php
class Mother {
   function sayHello() {
      echo "hello";
   }
   function greet() {
      self::sayHello(); // This will always call Mother::sayHello
      $this->sayHello();// This will call the sayHello method of whichever class this is an instance (Mother, or a sub-class)
   }
}

class Child extends Mother {
   function sayHello() {
      echo "hi";
   }
}

$child = new Child();
$child->greet(); // will print "hellohi"

Re: Does reversi tutorial misuse static methods?

Posted: 20 January 2021, 13:24
by Tisaac
Just to make even more precise, there is also the static:: prefix that will always call the static method of the children in case you are manipulating static methods (which I do a lot in BGA games)

Re: Does reversi tutorial misuse static methods?

Posted: 20 January 2021, 13:31
by galehar
I had the same issue using VS Code. You can't declare all your methods as static because of Gamestate, which can't be declared as static and so can't be used from a static method.
So I changed all my methods to $this-> and kept self:: for the framework methods (except for Gamestate and its methods) which I declared as static in a stub file. No more IDE warnings and I'm quite happy with the result.

Re: Does reversi tutorial misuse static methods?

Posted: 20 January 2021, 15:08
by Tisaac
galehar wrote: 20 January 2021, 13:31 I had the same issue using VS Code. You can't declare all your methods as static because of Gamestate, which can't be declared as static and so can't be used from a static method.
So I changed all my methods to $this-> and kept self:: for the framework methods (except for Gamestate and its methods) which I declared as static in a stub file. No more IDE warnings and I'm quite happy with the result.
You can turn the class into a singleton if you want to go full static.