1

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.

How do I get something like that working if it's possible (should be possible I think with too much hassle)?

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?

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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
Used you're idea of the toArray function, and 0scar's method of returning null elsewise
1

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;
    }
}

2 Comments

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; // Array echo $x -> id; // 'id' column from database
Well, I used your solution with grossvogel's idea of using a toArray function, thanks all
0

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

Comments

0

Use the PDOStatment class and it's rowCount property.

1 Comment

That's something I don't wanna do (in this case)

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.