-1
class tableBlock {
    var $table_border = '0';
    var $table_width = '100%';
    var $table_cellspacing = '0';
    var $table_cellpadding = '2';
    var $table_parameters = '';
    var $table_row_parameters = '';
    var $table_data_parameters = '';
    var $tableBox_string = '';

    function tableBlock($contents){
      $form_set = false;
...
      if ($form_set == true) $tableBox_string .= '</form>' . "\n";
      return $tableBox_string;
      }
  }

Currently this generates deprecation warnings every time its called because the function name and class name match of course that is only temporary.

This class is extended by many other classes in this website, and called within other functions as: $this->tableBlock($somearray); I've tried many ways of adding function __construct(){} without finding a way that maintains the function "tableBlock" also. every method tried yields a fatal error "call to nonexistent function". Anyone dealt with this successfully?

CBroe
  • 91,630
  • 14
  • 92
  • 150
teaman
  • 1
  • 2
  • This code should be completely refactored... PHP4-style everywhere oO – Honk der Hase Oct 12 '22 at 19:03
  • Well for this particular class, you could probably just add a constructor, that then itself just calls `tableBlock`. But can't say whether that will help you in regard to those "many other classes", that extend this one, we know too little about those at this point. Like for example, if those in turn also each have their own constructor that is named the same as the class itself, or in what context and how exactly they get instantiated, etc. – CBroe Oct 13 '22 at 08:14
  • Had tried that before coming here. Didn't work, same error. "class tableBox doesn't exist". – teaman Oct 13 '22 at 23:11

1 Answers1

0

To reduce code change, you can make this method private and create special __call magic method for that.

That way any time someone will call non-existing method tableBlock will hit __call proxy method.


class TableBlock {
    private string $table_border = '0';
    private string $table_width = '100%';
    private string $table_cellspacing = '0';
    private string $table_cellpadding = '2';
    private string $table_parameters = '';
    private string $table_row_parameters = '';
    private string $table_data_parameters = '';
    private string $tableBox_string = '';

    public function __call(string $name, array $arguments): mixed
    {
        if ($name === 'tableBlock') {
            return $this->makeTableBlock($arguments);
        }

        throw new \Exception('Call to undefined method ' . $name);
    }

    private function makeTableBlock($contents): string
    {
        $form_set = false;

        /* ... */

        if ($form_set == true) {
            $this->tableBox_string .= '</form>' . "\n";
        }

        return $this->tableBox_string;
    }
}

Justinas
  • 41,402
  • 5
  • 66
  • 96
  • Thanks Justinas, Your solution solved the issue. for this class. which was the only class that could not be resolved by adding a `__construct` function to the class. None of the classes that extended tableBox had within them a function matching the class name, so no rework was even needed on those. Much better solution than "refactoring" the entire website! – teaman Oct 13 '22 at 23:06