Return to Snippet

Revision: 38529
at January 4, 2011 22:11 by denright


Updated Code
<?php

interface FooInterface
{
    /**
     * Define a constant
     */
    const FOO = 'foo';
}

class Bar implements FooInterface
{
    /**
     * As noted in the PHP docs:
     *
     * An implementing class can 
     * not override an interface 
     * defined constant...
     */
    const FOO = 'bar'; // FATAL ERROR! 
}

class Baz extends Bar
{
    /**
     * However, a child of an 
     * implementing class can 
     * override an interface 
     * defined constant :)
     */
    const FOO = 'baz'; // WORKS!
}

Revision: 38528
at January 4, 2011 22:10 by denright


Updated Code
<?php

interface FooInterface
{
    /**
     * Define a constant
     */
    const FOO = 'foo';
}

class Bar implements FooInterface
{
    /**
     * As noted in the PHP docs:
     *
     * An implementing class can 
     * not override an interface 
     * defined constant :(
     */
    const FOO = 'bar'; // FATAL ERROR! 
}

class Baz extends Bar
{
    /**
     * However, a child of an 
     * implementing class can 
     * override an interface 
     * defined constant :)
     */
    const FOO = 'baz'; // WORKS!
}

Revision: 38527
at January 4, 2011 21:56 by denright


Updated Code
<?php

interface FooInterface
{
    /**
     * Define a constant
     */
    const FOO = 'foo';
}

class Bar implements FooInterface
{
    /**
     * An implementing class can 
     * not override an interface 
     * defined constant :(
     */
    const FOO = 'bar'; // FATAL ERROR! 
}

class Baz extends Bar
{
    /**
     * However, a child of an 
     * implementing class can 
     * override an interface 
     * defined constant :)
     */
    const FOO = 'baz'; // WORKS!
}

Revision: 38526
at January 4, 2011 21:51 by denright


Initial Code
<?php

interface FooInterface
{
    /**
     * Define a constant
     */
    const FOO = 'foo';
}

class Bar implements FooInterface
{
    /**
     * An mplementing class can 
     * not override an interface 
     * defined constant :(
     */
    const FOO = 'bar'; // FATAL ERROR! 
}

class Baz extends Bar
{
    /**
     * However, a child of an 
     * implementing class can 
     * override an interface 
     * defined constant :)
     */
    const FOO = 'baz'; // WORKS!
}

Initial URL


Initial Description
A class that implements an interface cannot override a constant defined in the interface. But any child class that extends from the implementing class can.

Initial Title
Interface-defined constants and implementing classes.

Initial Tags
php

Initial Language
PHP