|
From: why t. l. s. <wh...@ry...> - 2002-05-11 23:42:57
|
> I couldn't figure out why you use that eval statement there. Just: $m
> = new $mixed_name(); has worked fine for me before. (Possibly it
> didn't work because you didn't include the parentheses?)
I guess with or without parens work. I'm going to make that change in
Javuh.inc. The fewer evals the better.
> Also, you could try combining it with my technique for more "natural"
> mixins... That is, something like:
>
> class Bartender {
> function mixin( $based_on ) {
> $mixed_name = "mixed_class_" . get_class($this) . "_" . implode(
> "_",
> $based_on );
> if ( !class_exists( $mixed_name ) )
> create_mixed_class( $mixed_name, $based_on );
> // Possibly, include some method for serializing data that needs to
> be
> available to new object.
> // ex: $this = new $mixed_name(get_object_vars($this));
> $this = new $mixed_name();
> }
> }
It kinda scares the willies out of me that PHP can do this kind of stuff
(replacing $this inside of a method), but it's a neat hack to get an
instance to change classes. In fact, this helps me complete the entire
goal of the mixin article. Here's the new mixin() function now in
Javuh.inc. Works great.
/**
* Mixes an instance into a new class [Thanks, WorldMaker!]
* @param $o An object, the instance
* @param $based_on Classes to mix together
* @return Object
*/
function mixin( &$this, $based_on )
{
if ( !is_object( $this ) )
{
site_halt( 'Javuh,Mixin', 'Function <b>mixin()</b> should be
passed an object or <B>$this</B>.' ); }
array_unshift( $based_on, get_class( $this ) );
$mixed_name = "mixed_class_" . implode( "_and_", $based_on );
if ( !class_exists( $mixed_name ) )
create_mixed_class( $mixed_name, $based_on );
$vars = get_object_vars( $this );
$this = new $mixed_name;
foreach( $vars as $k => $v )
{
$this->$k = $v;
}
}
> overload() is definitely something, and it will be nice to see it going
> mainstream. I know there have been plenty of times when I could have
> used it. Personally, I think all classes should be overload()d in such
> that if a variable/function is not defined it should cause a "404" and
> seek out such functions, or at least, if you define such functions in a
> class, it should automatically be overload()d.
I played with the overload() function and found it sort of cool. I wish
it gave me a context for how the variable is being used. I solved my
initial problem by simply having functions named the same as my member
variables. I'll get into it sometime soon...
So all the mixin stuff we could hope for is now implemented and
functioning like a champ without any additional compile flags to PHP.
Javuh 1.0.2 is on its way, people!
|