views:

1255

answers:

4

Hi,

Currently I'm programming a database class which makes a little bit use of PHP's PDO class, but I'd like to add some simple features for making programming a certain application a bit easier.

Now In the following piece of pseudo code you can see where I'm going. The only problem in this example is that the $result variable is an object, which cannot be used for comparisation of some stuff I'm doing further on in the script:

<?php

class Database
{
 public function FetchRow ( $query )
 {
  // .. do some stuff, and make a $result variable
  return DatabaseStatement ( $result );
 }
}

class DatabaseStatement
{
 private $result;

 public function __construct ( $query )
 {
  // .. save result in property etc.
 }

 public function __get ( $column )
 {
  // .. check result item

  return $this -> result [ $column ];
 }
}

$db     = new Database;
$result = $db -> Query ( 'SELECT * FROM users WHERE id = 1;' );

if ( $result != null ) // Here $result should be an array OR null in case no rows are returned
{
 echo $result -> username; // Here $result should call the __get method
 echo '<pre>' , print_r ( $result ) , '</pre>'; // Here $result should be the array, cause it wasn't null just yet
}

As you can see the $result variable should not be an object when I'm doing a comparisation, I know it can be made to a string using __toString. But I'd like it to be some other type, mostly an array or null.

Now my question is simple: how do I get something like that working if it's possible (should be possible I think with too much hassle)?

I already did some searches on Google, but no result, and proberbly I'm not using the correct keywords, I don't really know what to search for.

So can somebody point me in the right direction, or possibly give a piece of code that should work or I can change to fit in my current class?

Thanks so far..!

Stefan

A: 

That's not possible. PHP doesn't allow you to overload operators.

troelskn
A: 

I think you'll have to do this in the same place you create the DatabaseStatement. So for instance:

    public function FetchRow ( $query )
    {
            // .. do some stuff, and make a $result variable
            $ds = DatabaseStatement ( $result );
            if($ds)
                return $ds;
            else
                return null;
    }
0scar
Yeah, I tried that one before, the problem still is that when you're returning $ds (in your code), it still can't use __get() AND be an array when using just te class variable. Like:$x = $y -> FetchRow();echo $x; // Arrayecho $x -> id; // 'id' column from database
Stefan
Well, I used your solution with grossvogel's idea of using a toArray function, thanks all
Stefan
A: 

Use the PDOStatment class and it's rowCount property.

erenon
That's something I don't wanna do (in this case)
Stefan
+2  A: 

It seems to me that you just need to add some methods that do what you want. Instead of forcing the $result object to be an array or null to check whether it's empty, why don't you just create and call a method isEmpty () that tells you what you want to know?

And if you need an array, create a method toArray () that returns what you want. OR, even better, make your object implement Iterator and/or ArrayAccess from the Standard PHP Library.

grossvogel
Well.. that seems to be 'the' solution at the moment, the reason I wanted to make it work like in my example code is that it looks a bit neater in my opinion, too bad something like that ain't possible
Stefan
Used you're idea of the toArray function, and 0scar's method of returning null elsewise
Stefan