PHP Class return object

Mohamed Abowarda
Mohamed Abowarda used Ask the Experts™
on
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?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2016

Commented:
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");
Most Valuable Expert 2011
Top Expert 2016

Commented:
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
Mohamed AbowardaSenior Software Engineer

Author

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.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Most Valuable Expert 2011
Top Expert 2016

Commented:
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
Most Valuable Expert 2011
Top Expert 2016
Commented:
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

Mohamed AbowardaSenior Software Engineer

Author

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?
Most Valuable Expert 2011
Top Expert 2016

Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial