Current class name for use with ::

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andy Jeffries

    #1

    Current class name for use with ::


    OK, assuming the following class structure:

    class A {
    public function printConst()
    {
    print {something}::TE ST_CONST;
    }
    }

    class B extends A {
    const TEST_CONST = 10;
    }

    class C extends A {
    const TEST_CONST = 20;
    }

    So, I want to be able to do as follows:

    $b = new B();
    $b->printConst() ;
    $c = new C();
    $c->printConst() ;

    What I want to know is what should go instead of {something}. I don't
    want the class it's declared in, but the class it's being called from.

    I'm sure there's a way to do it, I remember reading it when studying for
    the ZCE, but I'm damned if I can find it now when I need it.

    Cheers,


    Andy


    --
    Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
    http://www.gphpedit.org | PHP editor for Gnome 2
    http://www.andyjeffries.co.uk | Personal site and photos

  • Andy Jeffries

    #2
    Re: Current class name for use with ::

    On Wed, 03 May 2006 12:06:49 +0000, Andy Jeffries wrote:[color=blue]
    > What I want to know is what should go instead of {something}. I don't
    > want the class it's declared in, but the class it's being called from.[/color]

    It's OK, I found the answer (in Advanced PHP Programming by George
    Schlossnagle in case anyone cares).

    The answer is "self"

    Cheers,


    Andy

    --
    Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
    http://www.gphpedit.org | PHP editor for Gnome 2
    http://www.andyjeffries.co.uk | Personal site and photos

    Comment

    • Ciaran

      #3
      Re: Current class name for use with ::

      Andy,

      I think that 'self' will not do what you want, in this instance.

      In PHP, 'self' always refers to the class in which it is used so in
      your example code you will get a warning that A::TEST_CONST is not
      defined when you try either $b->printConst() or $c->printConst() .

      This is very annoying behaviour, but there isn't a keyword in PHP5 that
      refers to 'the current class' in the way you want it to. This is under
      discussion for PHP6, and a new keyword apparently may or may not be
      introduced - I'm hoping very much that it will!

      See:
      PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


      Comment

      • Andy Jeffries

        #4
        Re: Current class name for use with ::

        On Wed, 03 May 2006 07:20:28 -0700, Ciaran wrote:[color=blue]
        > I think that 'self' will not do what you want, in this instance.[/color]

        Yeah, I found that out after posting...
        [color=blue]
        > This is very annoying behaviour, but there isn't a keyword in PHP5 that
        > refers to 'the current class' in the way you want it to. This is under
        > discussion for PHP6, and a new keyword apparently may or may not be
        > introduced - I'm hoping very much that it will![/color]

        You're not the only one.

        For now, I've used a member variable. The code's only for my use anyway
        (it's not being distributed and I'm the only dev on the team) so it's
        fine, but I'd have preferred to keep it all neat.

        Thanks for the post though.

        Cheers,


        Andy


        --
        Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
        http://www.gphpedit.org | PHP editor for Gnome 2
        http://www.andyjeffries.co.uk | Personal site and photos

        Comment

        • Chung Leong

          #5
          Re: Current class name for use with ::

          Andy Jeffries wrote:[color=blue]
          > OK, assuming the following class structure:
          >
          > class A {
          > public function printConst()
          > {
          > print {something}::TE ST_CONST;
          > }
          > }
          >
          > class B extends A {
          > const TEST_CONST = 10;
          > }
          >
          > class C extends A {
          > const TEST_CONST = 20;
          > }
          >
          > So, I want to be able to do as follows:
          >
          > $b = new B();
          > $b->printConst() ;
          > $c = new C();
          > $c->printConst() ;
          >
          > What I want to know is what should go instead of {something}. I don't
          > want the class it's declared in, but the class it's being called from.
          >
          > I'm sure there's a way to do it, I remember reading it when studying for
          > the ZCE, but I'm damned if I can find it now when I need it.[/color]

          You can, of course, obtain the class name of the instance by calling
          get_class() on $this. The $class::member construct is illegal though,
          you'll end up with something like:

          class A {
          public function printConst()
          {
          $class = get_class($this );
          eval("print self::TEST_CONS T;");
          }

          }

          Comment

          • Jerry Stuckle

            #6
            Re: Current class name for use with ::

            Ciaran wrote:[color=blue]
            > Andy,
            >
            > I think that 'self' will not do what you want, in this instance.
            >
            > In PHP, 'self' always refers to the class in which it is used so in
            > your example code you will get a warning that A::TEST_CONST is not
            > defined when you try either $b->printConst() or $c->printConst() .
            >
            > This is very annoying behaviour, but there isn't a keyword in PHP5 that
            > refers to 'the current class' in the way you want it to. This is under
            > discussion for PHP6, and a new keyword apparently may or may not be
            > introduced - I'm hoping very much that it will!
            >
            > See:
            > http://www.php.net/~derick/meeting-n...different-name
            >[/color]

            Actually, Ciaran, in this case it makes sense. For instance, what if you had:

            $a = new A();
            $a->printConst() ;

            What would it print?


            Or, if you had:

            class A {
            public function printConst()
            {
            print {something}::TE ST_CONST;
            }
            }

            class B extends A {
            const TEST_CONST = 10;
            }

            class C extends B {
            const TEST_CONST = 20;
            }

            $c = new C();
            $c->printConst() ;

            What would it print?

            PHP does have an advantage over other OO languages such as Java and C++ in that
            it is a strictly interpreted language - it's not compiled. So something like
            this *could* be done.

            However, the way to handle this would be to define TEST_CONST in A, and set it
            in the constructors for B and C. Unfortunately, PHP doesn't allow a constant to
            be initialized in the constructor, so it has to be a non-constant. However,
            something like this works:

            class A {
            private $TEST_CONST = 30;

            public function A($tc) {
            $this->TEST_CONST = $tc;
            }
            public function printConst()
            {
            print $this->TEST_CONST;
            }
            }

            class B extends A {
            public function B () {
            parent::A(10);
            }
            }

            class C extends B {
            public function C() {
            parent::A(20);
            }
            }




            --
            =============== ===
            Remove the "x" from my email address
            Jerry Stuckle
            JDS Computer Training Corp.
            jstucklex@attgl obal.net
            =============== ===

            Comment

            Working...