0

I'm not familiar with php OOP and now I'm in some trouble. I'm developing my project in structured programming (not OOP) and I have included a class (made by someone else) and within my structured function I must initialize the class and do things. If you don't understand what I have typed here, please watch the code:

<?php
require __DIR__ . '/SourceQuery/SourceQuery.class.php';

function get_server_info($ip, $port){

    define( 'SQ_SERVER_ADDR', $ip );
    define( 'SQ_SERVER_PORT', $port );
    define( 'SQ_TIMEOUT',     5 );
    define( 'SQ_ENGINE',      SourceQuery :: GOLDSOURCE );

    $Query = new SourceQuery( );

    try
    {
        $Query->Connect( SQ_SERVER_ADDR, SQ_SERVER_PORT, SQ_TIMEOUT, SQ_ENGINE );

        $result = (array)$Query->GetInfo();

    }
    catch( Exception $e )
    {
        $result = array('Players' => 0, 'MaxPlayers' => 0);
    }

    $Query->Disconnect( );
    unset($Query);
    return array('players' => $result['Players'], 'max_players' => $result['MaxPlayers']);
}
?>

Basically I have created a function called get_server_info which constructs a SourceQuery object. I'm querying Counter-Strike servers for number of players and max players.

I don't know if this is correct programatically (mixing structured with OOP). I don't want to change code or create methods in the class, I want to use my function. I'm getting result for each call (get_server_info()), but the problem is every result is same !

For example:

for($i = 0; $i < 30; $i++){
   $result = get_server_info('66.55.44.3'.$i, 27015);
   echo $result['players'].' - '.$result['max_players'].'<br />';
}

The result is the same for any server ! (Yes, they are all up and running). When I'm checking the servers on my server viewer program they have different number of players, but my result from this loop is the same ! I don't know why.

If I query manually one by one I'm getting the correct result.

I think it's because the object isn't deleted or .... I don't know.

2
  • 1
    Where do you exactly have this which constructs a SourceQuery object in your code? Commented Mar 20, 2013 at 21:31
  • Sorry, I forget to add the code Commented Mar 20, 2013 at 21:35

2 Answers 2

1

I think it's because you're trying to overwrite a constant. (define)

Define the constants somewhere in your code outside any function and pass $id and $port directly to the class. And where do you have $Query initialized?

Try:

define( 'SQ_TIMEOUT',     5 );
define( 'SQ_ENGINE',      SourceQuery :: GOLDSOURCE );

function get_server_info($ip, $port){

    try
    {
        $Query->Connect( $ip, $port, SQ_TIMEOUT, SQ_ENGINE );
Sign up to request clarification or add additional context in comments.

1 Comment

Now it's working! The problem it was caused because I have used constants.
1

You forgot

$Query = new SourceQuery( );

See here an example.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.