Forum Moderators: coopster

Message Too Old, No Replies

how do i make a class return false when "new object" fails

         

mexicoshanty

8:27 am on Jul 2, 2005 (gmt 0)

10+ Year Member



hey everyone,

I'm trying to findout how to make a class that will return false when new object is called and fails. So i could use the object in the following situation.

if (!$object = new Object())
{
die("error message");
}

An idea's?

Coen

mexicoshanty

12:15 pm on Jul 2, 2005 (gmt 0)

10+ Year Member



Just to make it a little bit clearer.

I hope to make class like

class student
{
public funcion student($student_id)
{
if(student not in data base)
{
return false or something similar
}
}
}

then use it like this
if (!$student = new student($$student_id))
{
do what ever happens when there's an error
}

dreamcatcher

5:38 pm on Jul 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



class student
{
public function student($student_id)
{
$query = mysql_query("SELECT * FROM students WHERE id = '$student_id' LIMIT 1");

if (mysql_num_rows($query)==0)
{
return false;
}
else
{
return $whatever;
}
}
}

Then when you create the class do:

$student = new student();

if (!$student->student($id))
{
//no student exists
}
else
{
//hello there
}

dreamcatcher

6:05 pm on Jul 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As an added note I would mention that declaring a function as public is only compatible with PHP5>. If your server has 5 installed, no worries, but if you are testing on an intranet server then uploading after could be a problem if the production server has PHP4.

dc

mexicoshanty

3:42 am on Jul 3, 2005 (gmt 0)

10+ Year Member



thanks for replying, but i'm trying to use it in a constructor method.

class student extends user
{
public funcion __constructor($student_id)
{

Set $this-> vars needed to distinguish the "student" class from other classes, that also extend the "user" class

parent :: __construct($student);

}
}

// The parent constructor mentod does the following

class user
{
public funcion __constructor($user_id)
{
if (mysql_num_rows($query)==0)
{
return false or something similar
}
else
{
set a whole bunch of $this-> vars with query result
}
}
}

then use
if (!$student = new student($student_id))
{
// Student does not exist
}

ergophobe

3:36 pm on Jul 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What I usually do is set the student/member id to 0 if I don't find them in the DB. If you want to know whether it's a student or not, create a property or method for isStudent.

$student = new Student($id);
if (!$student->id)
{
echo "Sorry, that person is in our DB";
}

otherwise

$person = new Person($id);

if (!$person->isStudent())
{
echo "Sorry, Jethro Bodine is not a student";
}

mexicoshanty

4:52 am on Jul 4, 2005 (gmt 0)

10+ Year Member



Thanks, sounds like a plan

bennymack

1:23 pm on Jul 4, 2005 (gmt 0)

10+ Year Member



Hello.

It's impossible to return a value from a constructor. What you might want to do is utilize PEAR and if an error occurs in your constructor, constuct an error object. Then in your code, test the object with the:
if (PEAR::isError($object)) {
// do something, or don't
}

Of if you don't feel like using pear, just use the PHP is_a() [us3.php.net] function to test the object after it's created.