Reply to Re: [PHP] which class it is?

Your name:

Reply:


Posted by Jochem Maas on 03/04/05 15:23

anirudh dutt wrote:
> On Fri, 04 Mar 2005 12:37:55 +0100, Jochem Maas <jochem@iamjochem.com> wrote:
>
>>anirudh dutt wrote:
>>
>>>create a debug function (public) and use get_class. call that in the
>>
>>if you are using the 'public' keyword then you have to use php5,.....
>
>
> i meant a function like this:
> in a globally included file on a server far, far away
> function debugme ($obj, $mode = 'echo')
> {
> $obj_class = 'Class: '. get_class($obj);
> $obj_class.= 'non static properties: '. count($obj);
> if ($mode == 'echo') {
> echo $obj_class;
> } else {
> return $obj_class;
> }
> }
>
> won't debugme be available to all functions/methods?

any function (outside of a class definition of course) you define is available
everywhere yes.

have you tried:

var_dump( $obj );
print_r( $obj );
print_r( $obj, true );
var_export( $obj );
var_export( $obj, true );

also have a look at exceptions again: e.g.

DebugMe extends Exception()
{
private $obj;

/* ctor */
function __construct($obj, $msg = '', $code = 0 /* er? */)
{
$this->obj = $obj;
parent::__construct($msg, $code);
}

/* output/return human readable information about the contained object. */
public function printObj($returnVal = false)
{
$v = print_r( $obj, true );
if ((boolean)$returnVal) { return $v; }
echo $v;
}

/* overload getMessage() in base class ??? */
public function getMessage() {

return (($om = parent::getMessage())?"{$om}\n--\n":'')
. $this->printObj( true );
}
}

then in you code you can do stuff like:

try {
// do some stuff
// ....

try {

// ...
if ($somethingIsScrewed === true) {
throw new DebugMe( $myScrewedUpObject );
}
} catch (MyDBException) {
// catch a database related exception
}

// do some stuff
// ....

// ...
}
catch (MyAppException $e) {
// this is a very specific exception you wish to take care of here
}
catch (DebugMe $e) {
// this may output something or add a message to a queue/log etc.
handleDebugMeException( $e );
// maybe after outputting the information you want to throw
// a new exception
}
catch (Exception $e) {
// general exception catcher, you may just want to
// re-throw the exception into the parent try/catch block.
throw $e;
}

'Exception' is a php internal class, the rest I have made up,
the above is totally untested - its just to give you some ideas.

Using a exception does not determine what you do with the object or even how
you deal with it but it does give you a lot of control over where and when you
deal with (in your code).

I like Exceptions, you may very well come to hate them with a passion yet
to be seen in this solarsystem - there's no accounting for taste :-).

>
>
>>>u may wanna pass by reference and make it const (won't have to use
>>
>>if you're using php5 all objects are references, pass-by-reference occurs
>>whether you like it or not (unless you specifically use 'clone')
>>
>>...also a constant (global or class scope) cannot be set to an object.
>
>
> oh well :(
>
> btw, can't that be done in c++?

probably - that doesn't make a good thing by definition tho. besides php
is definitely not c++, on top of which php itself is written in c, which isn't
c++ either ;-)

>
> forgot about the pass by reference default-ness in php5

it just is, there is no alternative, other than to run in
zend1 compatibility mode (can't remember the ini setting name) which gives you
the php4 object behaviour but that totally defeats the purpose, and I don't
really consider it 'proper' php5 (its nice when your moving a complex app from 4 to 5)

and its just sooooo much nicer :-)

>

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация