N
N
niko832011-02-15 02:31:27
PHP
niko83, 2011-02-15 02:31:27

Code organization, pattern, php?

there is some method, in addition to the result of the work of this method, I want to get some kind of information,
for example, here is the code:

public function parserText($a) {<br/>
 ...<br/>
 return $b<br/>
}

but as a result of the code, for example, the text $a was too long and we cut it to 100 characters. Or we processed $a and returned $b, but when processing the code, we processed $a as if it were in XXX encoding
. So the question is how to collect such related information about the script's work.
return return (array($b, $infoMessage)); I don’t want to - too cool changes in the code, and the interface is broken,
I can also implement transfer by reference into the method and after the script is running, see what appeared in $infoMessage
public function parserText($a, &$infoMessage) {<br/>
 ...<br/>

What other good solutions can you recommend for this problem?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey Beresnev, 2011-02-15
@sectus

You can see how it is implemented by others with errors.
1. Receive an error by a separate method. For example: mysql_error - get the error text of the last operation.
2. And your way with a link. For example: fsockopen - 3 and 4 arguments are passed by reference and contain the error code and text, respectively.
But I like the first one better - no need to change the interface of existing methods.

G
Gibbzy, 2011-02-15
@gibbzy

Well, you can do it like this:
class Parser
{
protected $_messages = array();
public function parse($a)
{
$this->_messages['message key'] = 'some message ...';
/// some code;
return $b;
}
public function getMessages()
{
return $this->messages;
}
}
$parser = new Parser();
$parser->parse($a);
$messages = $parser->getMessages();

P
prairie_dog, 2011-02-15
@prairie_dog

You can return an object, and use the toString magic method . the old code will work (it also worked with the received variable as a string), and in the new code you can access this object and call methods that return additional information. This, of course, is IMHO and you should look at the code =) Good luck!
public function __toString()
{
return $this->output;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question