Jump to content

some oop theory questions


scott212

Recommended Posts

I've had a few rounds with some classes and I'm loving it. But I've gotten to the point where I've got some theory questions. When I purposely make a mistake inside one of the functions below, the error doesn't report if the die statement is on the function call like below. How would err handling usually be handled inside of a class? Also, does anyone see anything I'm doing majorly wrong or innefficient?

Here's the class structure that sparked it:

[code]<?php
class do_stuff {

  var error = 0;
  var message;

function get_input ($input_1, $input_2) {
  $this->var_1 = $input_1;
  $this->var_2 = $input_2;
}

function method_1() {
  do stuff here;
}

function method_2() {
  do stuff here;
}

function err_handler($message) {
  $this->error = 1;
  $this->message = $message;
}

function main() {
  $this->method_1() or die(err_handler("error on method_1"));
  $this->method_2() or die(err_handler("error on method_2"));

  $output = array($this->error, $this->message);
  return $output
}

}
?>[/code]
and of course the call:
[code]<?php
$doClass = new do_stuff;
$doClass->get_input($a, $b);
$return = $doClass->main;

echo $return[0]."<br>".$return[1];
?>[/code]

Link to comment
https://forums.phpfreaks.com/topic/34361-some-oop-theory-questions/
Share on other sites

IMO errors should allways be handled by the client code, not by the object itself. In php4 your methods should return true / false, and If you like you could also create an error() method which might display the last error. In php5 you should throw exceptions and have your client code handle them.
[quote author=scott212 link=topic=122590.msg505848#msg505848 date=1168926278]
[quote]In php5 you should throw exceptions and have your client code handle them.[/quote]

not sure what he means by 'throw exceptions'
[/quote]

[url=http://us2.php.net/manual/en/language.exceptions.php]http://us2.php.net/manual/en/language.exceptions.php[/url] :)

useful stuff

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.