1

I am writing a bit of PHP to implement ELO between soccer teams. I am using the ELO class found here: https://github.com/Chovanec/elo-rating and I am not modifying that code at all.

This is something that is driving me insane, I am clearly missing something obvious but I have no idea what.

So in my PHP file: results.php I have the following:

<?php
    require_once('Rating.php');

And then it goes into the code, always giving an error when it comes to me implementing the Rating class:

$r = new Rating($teamA,$teamB,1,0);

To which I get the following error:

Fatal error: Class 'Rating' not found in /home/XXXXX/public_html/soccer/results.php on line 65

So, to try and bug check I go all the way back to the top and use the following:

<?php
    require_once('Rating.php');



    if (class_exists('Rating')) {
        echo "Found";
    } else {
        echo "Not found";
    }
    die();

To which I get "Not found"

The Rating.php file opens as follows:

<?php

/**
 * This class calculates ratings based on the Elo system used in chess.
 *
 * @author Michal Chovanec <michalchovaneceu@gmail.com>
 * @copyright Copyright © 2012 - 2014 Michal Chovanec
 * @license Creative Commons Attribution 4.0 International License
 */

namespace Rating;

class Rating
{

I've confirmed the file is getting loaded correctly.

What am I missing?

7
  • The Rating.php file is in the same directory. I've renamed it to confirm that I get an error that it can't be found. So I know the file is found by the system. Apr 2, 2016 at 15:39
  • Show your Rating.php start code (until Rating class declaration). Namespace issue?
    – fusion3k
    Apr 2, 2016 at 15:40
  • Its having name space issue is namespace Apr 2, 2016 at 15:41
  • Added the Rating.php code excerpt. Apr 2, 2016 at 15:42
  • 1
    Try with new Rating\Rating($teamA,$teamB,1,0) Apr 2, 2016 at 15:43

1 Answer 1

5

Try calling it like this:

$r = new Rating\Rating($teamA,$teamB,1,0);

Or, alternately, remove the namespace from the Rating class.

Your main code is in the root namespace "\", so you have to tell it you want to get into the Rating namespace.

You could also put use Rating; at the top of your index file, instead of adding the namespace before the class name - just don't do both.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.