PHP Class return object

By using the magic method __toString() in class, I can make the PHP class instance return a string value, however, I want to return object instead of string.

Here is example

In the class:
$sql = mysql_query("SELECT * FROM users");

Open in new window

I want the class to return $sql

Then I can use the following to get $sql
$myClass = new ClassName();

Open in new window


Now, $myClass should be $sql

How can I do that?
LVL 12
Mohamed AbowardaSoftware EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
The class is used to create an object.  This statement is used to create a resource (or FALSE on failure) inside the variable named $sql:
$sql = mysql_query("SELECT * FROM users");
0
Ray PaseurCommented:
You might find this useful.
http://php.net/manual/en/language.oop5.php

If you can show us a little bit more about what you're trying to achieve we may be able to offer a better suggestion.

Regards, ~Ray
0
Mohamed AbowardaSoftware EngineerAuthor Commented:
I am trying to make the class return RESOURCE, exactly as it work with __toString() but I want to return RESOURCE instead of string.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Ray PaseurCommented:
I think you can just return the $sql variable and the resource will be inside it, perhaps with a getter() method.  But a bigger question exists - why would you want to take this resource outside of the object?  Object-oriented programming is all about encapsulating the data and programming so that it is not exposed, except through formal interfaces.  And since the only real use of a resource variable is to get access to other data, it might make the most sense to let the class have all the moving parts associated with access to the data.
http://php.net/manual/en/function.mysql-query.php

I'll try to create a test case.  Note that the resource data type is not the same as the object data type.
http://php.net/manual/en/language.types.resource.php
0
Ray PaseurCommented:
Outputs resource(2) of type (mysql result)

So I guess you can do this, if you really want to.

All the best, ~Ray
<?php // RAY_temp_medo3777.php
error_reporting(E_ALL);


// CREATE A OBJECT TO RUN A QUERY AND TRY TO RECOVER THE RESOURCE OUTSIDE OF THE CLASS


Class Medo
{
    // THE CONSTRUCTOR
    public function __construct()
    {
        // CONNECTION AND SELECTION VARIABLES FOR THE DATABASE
		$db_host = "localhost"; // PROBABLY THIS IS OK
		$db_name = "??";        // GET THESE FROM YOUR HOSTING COMPANY
		$db_user = "??";
		$db_word = "??";

		// OPEN A CONNECTION TO THE DATA BASE SERVER
		if (!$db_connection = mysql_connect("$db_host", "$db_user", "$db_word"))
		{
		    $errmsg = mysql_errno() . ' ' . mysql_error();
		    echo "<br/>NO DB CONNECTION: ";
		    echo "<br/> $errmsg <br/>";
		}

		// SELECT THE MYSQL DATA BASE
		if (!$db_sel = mysql_select_db($db_name, $db_connection))
		{
		    $errmsg = mysql_errno() . ' ' . mysql_error();
		    echo "<br/>NO DB SELECTION: ";
		    echo "<br/> $errmsg <br/>";
		    die('NO DATA BASE');
		}
    } // END __construct


    // THE QUERY RUNNER
    public function runQuery($qry)
    {
        $res = mysql_query($qry);
        return $res;
    } // END runQuery


} // END Class Medo


// MAKE AN OBJECT
$x = new Medo;
$y = $x->runQuery('Show Tables');

// SEE WHAT WE GOT
var_dump($y);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mohamed AbowardaSoftware EngineerAuthor Commented:
Is there is any possibility to get the mysql resource of type (mysql result) by using the class name directly $sql = $myClass; without having to use a function/variable inside the class such as $myClass->sql?
0
Ray PaseurCommented:
I do not know of any, and I have never really thought about it -- it is not a design pattern that I would use.  As they mention on the PHP man page, resource variables hold special handlers to opened files, database connections, image canvas areas and the like, converting to a resource makes no sense and that is probably why there is no "magic method" to make the conversion.  See http://php.net/manual/en/language.oop5.magic.php
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.