Class Aliases

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

Consider the words Color and Colour.

Now, what if I wanted to have a PHP class for a color/colour, what's a good way to create an alias ?

My first thought is to create the class color and create an empty extender named colour.

Code: [ Select ]
class color
{
   // constructor, etc
}
class colour extends color {/*empty*/}
  1. class color
  2. {
  3.    // constructor, etc
  4. }
  5. class colour extends color {/*empty*/}


I would assume since colour is extending color with no additions, it would effectively be creating a low-cost pointer to the color class, giving me an alias and preventing arguments about whether the class should be named color or colour.

Anyone see any drawbacks to doing this ?
Is there a better way ?

I see there's a class_alias function, but there's no version information for it in the manual at this time for one, and it raises a fatal error for an undefined function on my copy of PHP-5.2.6 for two.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6252
  • Loc: Seattle, WA

Post 3+ Months Ago

If this were C++, just #define it. :)

I suppose as long as your class has a well-defined public interface, or if you were using an actual PHP interface to access it, then it wouldn't be a problem, but it still seems hackish to me.
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

Why would you want classes with different names but the same functionality? Sorry I am just trying to understand the requirements
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8397
  • Loc: USA

Post 3+ Months Ago

Have you tried running function_exists() to see if that function is available in your install?
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

Quote:
If this were C++, just #define it.


I tried this before making this thread, it didn't error out, but for some reason trying to instantiate using the defined alias threw a fatal error because "colour" is an undefined class.

Code: [ Select ]
define('colour', color);


Quote:
Why would you want classes with different names but the same functionality?


To put an end to pointless arguments about whether a class in a library should be named "color" or "colour". If both are supported that's the end of it. I'm sure it would come in handy for other words as well.

Quote:
Have you tried running function_exists() to see if that function is available in your install?

Didn't need to, I tried to use it and received a fatal error. That's a pretty good indicator that the function_exists is going to return false as well. :)
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8397
  • Loc: USA

Post 3+ Months Ago

joebert wrote:
Quote:
Have you tried running function_exists() to see if that function is available in your install?

Didn't need to, I tried to use it and received a fatal error. That's a pretty good indicator that the function_exists is going to return false as well. :)

Heh, my thick skull didn't take that in :lol:


I don't know what you are having trouble with. (That's if you are using PHP).

This is my test class...
Code: [ Select ]
<?php
class color {
    function say_apple()
    {
        echo 'apple';
    }
    function say_something($a)
    {
        echo $a;
    }
}
 
class colour extends color {
 
}
?>
  1. <?php
  2. class color {
  3.     function say_apple()
  4.     {
  5.         echo 'apple';
  6.     }
  7.     function say_something($a)
  8.     {
  9.         echo $a;
  10.     }
  11. }
  12.  
  13. class colour extends color {
  14.  
  15. }
  16. ?>

And this is my implementation of it
Code: [ Select ]
<?php
include 'class.php';
$color = new color();
$color->say_apple();
echo "<br />";
$color->say_something('I am saying this using color!!!');
echo "<br />";
echo "<br />";
echo "<br />";
$colour = new colour();
$colour->say_apple();
echo "<br />";
$colour->say_something('This is me using colour...');
?>
  1. <?php
  2. include 'class.php';
  3. $color = new color();
  4. $color->say_apple();
  5. echo "<br />";
  6. $color->say_something('I am saying this using color!!!');
  7. echo "<br />";
  8. echo "<br />";
  9. echo "<br />";
  10. $colour = new colour();
  11. $colour->say_apple();
  12. echo "<br />";
  13. $colour->say_something('This is me using colour...');
  14. ?>

That gave me
Quote:
apple
I am saying this using color!!!


apple
This is me using colour...

Worked fine with me.

[EDIT:]

or can't you do it all in __autoload();
Code: [ Select ]
<?php
function __autoload($class)
{
    $class_alias = array(
        'colour' => 'color'
    );
   
    if(file_exists("classes/$class.php"))
    {
        include("classes/$class.php");
    }
    else
    {
        if(file_exists("classes/{$class_alias[$class]}.php"))
        {
            include("classes/{$class_alias[$class]}.php");
        }
    }
}
?>
  1. <?php
  2. function __autoload($class)
  3. {
  4.     $class_alias = array(
  5.         'colour' => 'color'
  6.     );
  7.    
  8.     if(file_exists("classes/$class.php"))
  9.     {
  10.         include("classes/$class.php");
  11.     }
  12.     else
  13.     {
  14.         if(file_exists("classes/{$class_alias[$class]}.php"))
  15.         {
  16.             include("classes/{$class_alias[$class]}.php");
  17.         }
  18.     }
  19. }
  20. ?>

How does that sound? (I'm sure you're able to find out how to work that :lol: )
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

It works fine for me to bogey. :)

I'm more or less looking for input on it, and possible other methods of accomplishing class aliasing. :D
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8397
  • Loc: USA

Post 3+ Months Ago

I just edited my post... what about that? I edited before I saw your post to prevent double posting.

// I hate editing post a thousand times to either add a word, a little phrase, fix a grammatical or coding error or add some more info, and then find out there is another post under me...
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

Something along those lines looks like it would work.
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

LOL Ok well here are th problems. When you create an instance of a sub class it will automatically create a handle to it's parent class so you have two instance in memory as opposed to one.

I would think class aliasing would be the form of

Code: [ Select ]
//color
$color = new Color();

//colour
$colour = new Color();
  1. //color
  2. $color = new Color();
  3. //colour
  4. $colour = new Color();


the idea of implementing an interface means that you would end up with duplicate code though

Code: [ Select ]
public interface IRGBVisualRepresentation{

}

public class Color implements IRGBVisualRepresentation{
//members here
}

public class Colour implements IRGBVisualRepresentation{
//members heres
}
  1. public interface IRGBVisualRepresentation{
  2. }
  3. public class Color implements IRGBVisualRepresentation{
  4. //members here
  5. }
  6. public class Colour implements IRGBVisualRepresentation{
  7. //members heres
  8. }


Or you could give it the name I gave it above that implies it is a colour as I did here LOL RGBVisualRepresentation (I quiet like that :))
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

Quote:
When you create an instance of a sub class it will automatically create a handle to it's parent class so you have two instance in memory as opposed to one.


I don't understand.

I would think since the alias class has an empty definition, it would be using the definitions from the parent class.

I think I should dig into the PHP source and see how classes work under the hood. :scratchhead:

And why does it have to be "RGBVisualRepresentation", why can't it be "CMYKVisualRepresentation" ? ;)
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

Because I was assuming you would be instantiating the class in the context of rendering graphics on a computer which would be rgb, if it is a printer application then you can call it cmyk thingy

The way it works is effectively it creates an instance of Colour then because it has extended Color it needs to create an instance of that. The memory pointers would point to Color for method/function calls. So as you initially put it, it would be a low cost pointer but not as low as defining the Colour class as a stand alone class ( not extending another class)
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

So, defining the same class two times using two different names would take less resources than extending a class using an empty class definition as the extension ? :scratchhead:

That just seems odd, if I'm understanding this right.
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

well think about it

It might take more disk space but
Code: [ Select ]
$color = new Color() //supposing color extends colour


Under the hood it implictly creates an instance of Colour because you going to need access to it. Where as if it didn't extend anything it wouldn't create a reference to it :)

Make sense?
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6252
  • Loc: Seattle, WA

Post 3+ Months Ago

You can almost think of a subclass as a wrapper around a base class. Every time you create an instance of a subclass, an instance of the base class must be created in order to allocate storage for its member data and to allow access to its methods.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

I see what you're saying now.

That seems like a design flaw to me on the surface. I would think that an extending class would refer to the parent class as a sort of blueprint when being instantiated rather than create an instance of the parent class to work with.

I guess when I consider things like "parent::__construct() it starts to make more sense though.

Thanks. :D

Post Information

  • Total Posts in this topic: 16 posts
  • Users browsing this forum: No registered users and 132 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.