oop - php within a function call a constructor and initialize object -
I'm not familiar with php OOP and now I'm in some trouble. I am developing my project in structured programming (not OOP) and I have included a class (created by somebody else) and within my structured function, I should start and work class if you do not understand Here's what I have typed here, please see the code:
& lt ;? Php is required __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 source (); {$ Query-> Try (Connect SQ_SERVER_ADDR, SQ_SERVER_PORT, SQ_TIMEOUT, SQ_ENGINE); $ Result = (array) $ query- & gt; GetInfo (); } Hold (exception $ E) {$ result = array ('player' => gt; 'maxplayer' => gt; 0); } $ Query- & gt; Disconnect (); Not set ($ query); Returns array ('player' = & gt; $ result ['player'], 'max_player' = & gt; $ result ['maxplayer']); }? & Gt; Actually I have created a function called get_server_info, which creates a SourceQuery object. I am interrogating Counter Strike server for the number of players and maximum players. I do not know whether it is correct (structured blend with OP) I do not want to change the code or to make methods in the classroom, I want to use my function. I'm getting results for each call (get_server_info ()), but the problem is similar to everyone!
For example:
For ($ i = 0; $ I <30; $ i ++) {$ result = get_server_info ('66. 55.44.3 '. $ I, 27015); Returns $ [[player ']' result. '-'. $ Result ['max_players'].' & Lt; Br / & gt; ';;; } The result is similar to any server! (Yes, they all are up and running). When I'm checking the server on my server viewer program, they have different players, but this result is the same for me! I do not know why.
If I ask one by one manually, then I am getting the right result.
I think this is because the object has not been removed or ... I do not know.
I think this is because you are continuously trying to overwrite ( defined ) Define constants in your code anywhere on any function and directly $ id and $ port pass class And when do you have $ query initiated? Try:
Define ('SQ_TIMEOUT', 5); Define ('SQ_ENGINE', SourceQuery :: GOLDSOURCE); Function get_server_info ($ ip, $ port) {try {$ query- & gt; Connect ($ IP, $ Port, SQ_TIMEOUT, SQ_ENGINE);
Comments
Post a Comment