Does "->" have another meaning in PHP?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
swraman
Forum Commoner
Posts: 58
Joined: Thu Nov 06, 2008 12:33 am

Does "->" have another meaning in PHP?

Post by swraman »

I know it means to get an object from a certain class, but can it have another meaning?

Some code I'm working with has the line:

Code: Select all

     if ($result->GetNumRows() > 0) {
 
But in the entire site there is no class "result". There is a variable "result", however.

The code errors at this point.

Is this a blatant mistake, or is there something else that "->it can signify?
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Does "->" have another meaning in PHP?

Post by jaoudestudios »

The class does not have to be called result, as $result is the object not the class (instance of the class I think it is called).
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Does "->" have another meaning in PHP?

Post by Mark Baker »

swraman wrote:I know it means to get an object from a certain class, but can it have another meaning?
Nor does it necessarily return an object. It can call an object method that may or may not return a value (which could be of any data type), or read a property (which can also be of any data type) of that instantiated object... look for the presence or absence of braces, used when calling an object method but absent when accessing an object property.

In this case, it's calling a method called GetNumRows rather than accessing a property called GetNumRows.
swraman
Forum Commoner
Posts: 58
Joined: Thu Nov 06, 2008 12:33 am

Re: Does "->" have another meaning in PHP?

Post by swraman »

jaoudestudios wrote:The class does not have to be called result, as $result is the object not the class (instance of the class I think it is called).
Isht $result the class here? and GetNumRows() is the function in the class result that it is calling?
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Does "->" have another meaning in PHP?

Post by Mark Baker »

swraman wrote:Isht $result the class here?
No, $result is the variable name that you have given to the instance of a class that was passed back as a return value by your call to mysql_query()
e.g

Code: Select all

$result = mysql_query('SELECT * FROM table');
It isn't even necessary to know what the class is called: simply the public methods and properties that are defined within the class, and that you can access.
In this case, it's a class that is defined within the mysql extension; and the public methods and attributes are described in the PHP manual.
swraman wrote:and GetNumRows() is the function in the class result that it is calling?
That is correct
swraman
Forum Commoner
Posts: 58
Joined: Thu Nov 06, 2008 12:33 am

Re: Does "->" have another meaning in PHP?

Post by swraman »

Ok, i understand that. Now the error Im getting is

Fatal error: Call to a member function GetNumRows() on a non-object in C:\Program Files\BitNami WAPPStack\apache2\htdocs\updates\tracker\project.php on line 398

doesnt that mean that $result is not an instance of an object, or am I misunderstanding the error?
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Does "->" have another meaning in PHP?

Post by Mark Baker »

swraman wrote:Fatal error: Call to a member function GetNumRows() on a non-object in C:\Program Files\BitNami WAPPStack\apache2\htdocs\updates\tracker\project.php on line 398

doesnt that mean that $result is not an instance of an object
Correct, so what code are you using to set $result?
swraman
Forum Commoner
Posts: 58
Joined: Thu Nov 06, 2008 12:33 am

Re: Does "->" have another meaning in PHP?

Post by swraman »

Its rather complicated, ill try to explain (and thanks so much if anyone can help :) )

Code: Select all

global $database;
$result = $database->Query("SELECT status FROM tr_projects WHERE id = '{0}'", $id);
$database comes from:

Code: Select all

require_once('pgsql_database.php');
$database = new PGDatabase;
and PGDatabase comes from:

Code: Select all

class PGDatabase implements Database {
   /**
    * @access private
    * @var resource resource handle for the database connection
    */
   private $link;
   
   /**
    * @access private
    * @var int the current transation level
    */
   private $intrans;
   
   public function __construct() {
      $this->link  = false;
      $this->intrans = false;
   }
   
   ....(a bunch of other functions)...
   
   public function Query() {
      $args = func_get_args();
      $num = func_num_args();
      $query = array_shift($args);
 
      if (isset($args[0]) && is_array($args[0])) {
         // The rest of the parameters are passed in as an array
         $args = $args[0];
      }
      
      for ($i = 0; $i < $num - 1; ++$i) {
         $query = str_replace("{{$i}}", $this->EscapeString($args[$i]),
            $query);
      }
      
      $result = pg_query($this->link, $query);
 
      TR_POSTCONDITION($result, 'Query Failure:', $query,
                       pg_result_error($result));
      if (!is_bool($result)) {
        return new PGQueryResult($result, $this->link);
      }
   }
   
  ...(and more functions)....
}
and now, finally, PGQueryResult is defined:

Code: Select all

class PGQueryResult implements QueryResult {
   /**
    * @access private
    * @var resource resource handle to the MySQL query
    */
   private $result;
   
   /**
    * @access private
    * @var resource the link identifier for this database
    */
   private $link;
   
   public function __construct($result, $link) {
      TR_PRECONDITION($result, 'bad result');
      
      $this->result = $result;
      $this->link   = $link;
   }
   
   public function __destruct() {
      $this->FreeResult();
   }
   
...(more functions)...
   
   public function GetNumRows() {
      return pg_num_rows($this->result);
   }
 
(...more functions...)
 
}
 
So I dont see how $result isnt a class, it is defined as an instance of PGQuerryResult, isnt it?
Last edited by swraman on Wed Jan 14, 2009 6:17 pm, edited 1 time in total.
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: Does "->" have another meaning in PHP?

Post by Syntac »

A class is just a "template" for an object.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Does "->" have another meaning in PHP?

Post by Eran »

The problem is with the instancing logic:

Code: Select all

if (!is_bool($result)) {
             return new PGQueryResult($result, $this->link);
}
if $result here is false (ie query failed or returned no results), then no object would be returned, and you get the error you described earlier.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Does "->" have another meaning in PHP?

Post by califdon »

Yes, this is a very common error message. Nearly always the cause is a bad query. If the query that is supposed to return a result to the object ($result, in your case) fails (let's say, because a field name is incorrect), the object is never instantiated, so the error message says that you are referring to a non-object. Check your query very carefully. Try executing it on your database outside of your script.
swraman
Forum Commoner
Posts: 58
Joined: Thu Nov 06, 2008 12:33 am

Re: Does "->" have another meaning in PHP?

Post by swraman »

pytrin wrote:The problem is with the instancing logic:

Code: Select all

if (!is_bool($result)) {
             return new PGQueryResult($result, $this->link);
}
if $result here is false (ie query failed or returned no results), then no object would be returned, and you get the error you described earlier.
So I have to make the function Query return a value for the cases when $result is a boolean?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Does "->" have another meaning in PHP?

Post by Eran »

Or return the object regardless, and let the object tell the script if it has a result set or not. The main thing is to avoid a method being called on a non-object
swraman
Forum Commoner
Posts: 58
Joined: Thu Nov 06, 2008 12:33 am

Re: Does "->" have another meaning in PHP?

Post by swraman »

If I remove the conditional statement I get a load of

Code: Select all

Warning: pg_query() [function.pg-query]: Query failed: ERROR: current transaction is aborted, commands ignored until end of transaction block in C:\Program Files\BitNami WAPPStack\apache2\htdocs\updates\tracker\pgsql_database.php on line 187
 
Warning: pg_num_rows(): supplied argument is not a valid PostgreSQL result resource in C:\Program Files\BitNami WAPPStack\apache2\htdocs\updates\tracker\pgsql_database.php on line 83
 
Warning: pg_free_result(): supplied argument is not a valid PostgreSQL result resource in C:\Program Files\BitNami WAPPStack\apache2\htdocs\updates\tracker\pgsql_database.php on line 79
 
errors.  Is there another way I should tackle it?
 
If it helps, the entire error I get when i run the script (with the conditional statement in it):

Code: Select all

 
Warning: pg_query() [function.pg-query]: Query failed: ERROR: syntax error at or near ")" LINE 1: INSERT INTO tr_projects() VALUES() ^ in C:\Program Files\BitNami WAPPStack\apache2\htdocs\updates\tracker\pgsql_database.php on line 187
 
Warning: pg_query() [function.pg-query]: Query failed: ERROR: current transaction is aborted, commands ignored until end of transaction block in C:\Program Files\BitNami WAPPStack\apache2\htdocs\updates\tracker\pgsql_database.php on line 187
 
Notice: Undefined variable: data in C:\Program Files\BitNami WAPPStack\apache2\htdocs\updates\tracker\pgsql_database.php on line 199
 
Warning: pg_query() [function.pg-query]: Query failed: ERROR: current transaction is aborted, commands ignored until end of transaction block in C:\Program Files\BitNami WAPPStack\apache2\htdocs\updates\tracker\pgsql_database.php on line 187
 
Fatal error: Call to a member function GetNumRows() on a non-object in C:\Program Files\BitNami WAPPStack\apache2\htdocs\updates\tracker\project.php on line 398
swraman
Forum Commoner
Posts: 58
Joined: Thu Nov 06, 2008 12:33 am

Re: Does "->" have another meaning in PHP?

Post by swraman »

If you run the pg_query INSERT INTO tr_projects() VALUES(), is it suposed to error?
Post Reply