473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Overriding class constants

I want to provide a set of static functions in a superclass that work
with class constants defined in a decendant of that class.
Unfortunately I've run into a snag with this idea. Example:

class SuperClass
{
const CNST = 'Super class';

public static function getCnst ()
{
return self::CNST;
}
}

class SubClass extends SuperClass
{
const CNST = 'Sub class';
}

echo (SubClass::getCnst ());

I'd want the above code tou output "Sub Class", but instead it outputs
"Super class".

It would sem that static functions can't work with overridden
constants in subclasses, or am I missing something?
Jul 3 '08 #1
12 6088
Gordon wrote:
I want to provide a set of static functions in a superclass that work
with class constants defined in a decendant of that class.
Unfortunately I've run into a snag with this idea. Example:

class SuperClass
{
const CNST = 'Super class';

public static function getCnst ()
{
return self::CNST;
}
}

class SubClass extends SuperClass
{
const CNST = 'Sub class';
}

echo (SubClass::getCnst ());

I'd want the above code tou output "Sub Class", but instead it outputs
"Super class".

It would sem that static functions can't work with overridden
constants in subclasses, or am I missing something?
Nope, unfortunately, this is true.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Jul 3 '08 #2
..oO(Gordon)
>I want to provide a set of static functions in a superclass that work
with class constants defined in a decendant of that class.
Unfortunately I've run into a snag with this idea. Example:

class SuperClass
{
const CNST = 'Super class';

public static function getCnst ()
{
return self::CNST;
}
}

class SubClass extends SuperClass
{
const CNST = 'Sub class';
}

echo (SubClass::getCnst ());

I'd want the above code tou output "Sub Class", but instead it outputs
"Super class".

It would sem that static functions can't work with overridden
constants in subclasses, or am I missing something?
Currently static references are resolved at compile time. What you need
here is called late static binding and scheduled for PHP 5.3.

Micha
Jul 3 '08 #3
On Jul 3, 4:29 pm, Michael Fesser <neti...@gmx.dewrote:
.oO(Gordon)
I want to provide a set of static functions in a superclass that work
with class constants defined in a decendant of that class.
Unfortunately I've run into a snag with this idea. Example:
class SuperClass
{
const CNST = 'Super class';
public static function getCnst ()
{
return self::CNST;
}
}
class SubClass extends SuperClass
{
const CNST = 'Sub class';
}
echo (SubClass::getCnst ());
I'd want the above code tou output "Sub Class", but instead it outputs
"Super class".
It would sem that static functions can't work with overridden
constants in subclasses, or am I missing something?

Currently static references are resolved at compile time. What you need
here is called late static binding and scheduled for PHP 5.3.

Micha
Thanks.

Is there any kind of a workaround possible in older versions? The
only way I've found to get it to work as intended is to "inherit from
the clipboard" and cut and paste the methods into all the classes.
Maintainence nightmare.
Jul 4 '08 #4
On 4 jul, 10:14, Gordon <gordon.mc...@ntlworld.comwrote:
On Jul 3, 4:29 pm, Michael Fesser <neti...@gmx.dewrote:
.oO(Gordon)
>I want to provide a set of static functions in a superclass that work
>with class constants defined in a decendant of that class.
>Unfortunately I've run into a snag with this idea. *Example:
>class SuperClass
>{
* *const CNST * * *= 'Super class';
* *public static function getCnst ()
* *{
* * * * * *return self::CNST;
* *}
>}
>class SubClass extends SuperClass
>{
* *const CNST * * *= 'Sub class';
>}
>echo (SubClass::getCnst ());
>I'd want the above code tou output "Sub Class", but instead it outputs
>"Super class".
>It would sem that static functions can't work with overridden
>constants in subclasses, or am I missing something?
Currently static references are resolved at compile time. What you need
here is called late static binding and scheduled for PHP 5.3.
Micha

Thanks.

Is there any kind of a workaround possible in older versions? *The
only way I've found to get it to work as intended is to "inherit from
the clipboard" and cut and paste the methods into all the classes.
Maintainence nightmare.
Theoratically speaking, a constant that needs to change during
execution is not a constant.
Could you explain the context a bit more ? I don't really see what
you're trying to do in your "simple" example.
Jul 4 '08 #5
On Jul 4, 9:50 am, Mathieu Maes <mathieu.m...@gmail.comwrote:
On 4 jul, 10:14, Gordon <gordon.mc...@ntlworld.comwrote:
On Jul 3, 4:29 pm, Michael Fesser <neti...@gmx.dewrote:
.oO(Gordon)
I want to provide a set of static functions in a superclass that work
with class constants defined in a decendant of that class.
Unfortunately I've run into a snag with this idea. Example:
class SuperClass
{
const CNST = 'Super class';
public static function getCnst ()
{
return self::CNST;
}
}
class SubClass extends SuperClass
{
const CNST = 'Sub class';
}
echo (SubClass::getCnst ());
I'd want the above code tou output "Sub Class", but instead it outputs
"Super class".
It would sem that static functions can't work with overridden
constants in subclasses, or am I missing something?
Currently static references are resolved at compile time. What you need
here is called late static binding and scheduled for PHP 5.3.
Micha
Thanks.
Is there any kind of a workaround possible in older versions? The
only way I've found to get it to work as intended is to "inherit from
the clipboard" and cut and paste the methods into all the classes.
Maintainence nightmare.

Theoratically speaking, a constant that needs to change during
execution is not a constant.
Could you explain the context a bit more ? I don't really see what
you're trying to do in your "simple" example.
The values don't have to change during execution. They just have to
be different for different classes.

What I need is to be able to specify some parameters in a class that
describe how it is to be dealt with. For example for creating a new
item in the database I have a page that loads a HTML form for the user
to populate. However, each different type of item has a different
form. For example a document needs a field for content but a folder
doesn't as a folder is just a container for other items. I want to
specify in a class constant attributes for things such as that. For
example, a Folder class could have a NEW_FORM constant set to
'newfolder.php' whereas a Document class could have a NEW_FORM with a
value of 'newdoc.php'.

You have to be able to know what these values are before you can
instantise a class, however. Of course you can do a switch statement
based on the class of the item you intend to create, but that means
that, for example, I later want to add a new Blog class, then as well
as creating the new class I also have to remember to add Blog as an
option in the switch statement. If the Blog class itself can define
what makes it different from the other classes the system has to work
with then I can elimate the switch statement.

Of course there is only one switch statement in the above example but
in the real app there is more than one script that has to deal with
different types of object. If I used switch statements in all of
those cases then the result would have poor maintainability.

As was stated earlier by another poster the next version of PHP will
have a solution to this problem, namely late static binding. I guess
it's a common enough problem that they needed to add that feature to
the new release. Of course PHP 5.3 could be days off or several
months off and was hoping to find a workaround in the meantime.
Jul 4 '08 #6
Gordon wrote:
On Jul 4, 9:50 am, Mathieu Maes <mathieu.m...@gmail.comwrote:
>On 4 jul, 10:14, Gordon <gordon.mc...@ntlworld.comwrote:
>>On Jul 3, 4:29 pm, Michael Fesser <neti...@gmx.dewrote:
.oO(Gordon)
I want to provide a set of static functions in a superclass that work
with class constants defined in a decendant of that class.
Unfortunately I've run into a snag with this idea. Example:
class SuperClass
{
const CNST = 'Super class';
public static function getCnst ()
{
return self::CNST;
}
}
class SubClass extends SuperClass
{
const CNST = 'Sub class';
}
echo (SubClass::getCnst ());
I'd want the above code tou output "Sub Class", but instead it outputs
"Super class".
It would sem that static functions can't work with overridden
constants in subclasses, or am I missing something?
Currently static references are resolved at compile time. What you need
here is called late static binding and scheduled for PHP 5.3.
Micha
Thanks.
Is there any kind of a workaround possible in older versions? The
only way I've found to get it to work as intended is to "inherit from
the clipboard" and cut and paste the methods into all the classes.
Maintainence nightmare.
Theoratically speaking, a constant that needs to change during
execution is not a constant.
Could you explain the context a bit more ? I don't really see what
you're trying to do in your "simple" example.

The values don't have to change during execution. They just have to
be different for different classes.

What I need is to be able to specify some parameters in a class that
describe how it is to be dealt with. For example for creating a new
item in the database I have a page that loads a HTML form for the user
to populate. However, each different type of item has a different
form. For example a document needs a field for content but a folder
doesn't as a folder is just a container for other items. I want to
specify in a class constant attributes for things such as that. For
example, a Folder class could have a NEW_FORM constant set to
'newfolder.php' whereas a Document class could have a NEW_FORM with a
value of 'newdoc.php'.

You have to be able to know what these values are before you can
instantise a class, however. Of course you can do a switch statement
based on the class of the item you intend to create, but that means
that, for example, I later want to add a new Blog class, then as well
as creating the new class I also have to remember to add Blog as an
option in the switch statement. If the Blog class itself can define
what makes it different from the other classes the system has to work
with then I can elimate the switch statement.

Of course there is only one switch statement in the above example but
in the real app there is more than one script that has to deal with
different types of object. If I used switch statements in all of
those cases then the result would have poor maintainability.

As was stated earlier by another poster the next version of PHP will
have a solution to this problem, namely late static binding. I guess
it's a common enough problem that they needed to add that feature to
the new release. Of course PHP 5.3 could be days off or several
months off and was hoping to find a workaround in the meantime.
But in your example, your inheritance hierarchy is incorrect. In folder
is not a "typeof" document. And no, physical storage media doesn't count.

Derived classes should always be more specific examples of the base class.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Jul 4 '08 #7
On Jul 4, 4:14*am, Gordon <gordon.mc...@ntlworld.comwrote:
On Jul 3, 4:29 pm, Michael Fesser <neti...@gmx.dewrote:
.oO(Gordon)
>I want to provide a set of static functions in a superclass that work
>with class constants defined in a decendant of that class.
>Unfortunately I've run into a snag with this idea. *Example:
>class SuperClass
>{
* *const CNST * * *= 'Super class';
* *public static function getCnst ()
* *{
* * * * * *return self::CNST;
* *}
>}
>class SubClass extends SuperClass
>{
* *const CNST * * *= 'Sub class';
>}
>echo (SubClass::getCnst ());
>I'd want the above code tou output "Sub Class", but instead it outputs
>"Super class".
>It would sem that static functions can't work with overridden
>constants in subclasses, or am I missing something?
Currently static references are resolved at compile time. What you need
here is called late static binding and scheduled for PHP 5.3.
Micha

Thanks.

Is there any kind of a workaround possible in older versions? *The
only way I've found to get it to work as intended is to "inherit from
the clipboard" and cut and paste the methods into all the classes.
Maintainence nightmare.
One workaround that works with instances of a class (which
unfortunately does not work with static calls like in your example) is
to use the get_class function to grab the class name of the current
object, and then build a string you can pass to the constant function.

However, since you want static functions, the only way I know of to
work around the problem is to cheat: override the getConst function in
SubClass and pass in the class name to the SuperClass function, which
will give you something like this:

class SuperClass
{
const CNST = 'Super class';

public static function getCnst ($class = NULL)
{
if (!$class)
{
$class = get_class();
}
return constant($class . '::CNST');
}

}

class SubClass extends SuperClass
{
const CNST = 'Sub class';

public static function getCnst ()
{
return parent::getCnst(get_class());
}

}

echo SubClass::getCnst();
// outputs "Sub class"

You somewhat avoid the "inherit by clipboard" maintenance issue, but
you still have to override the getCnst method in all of your
subclasses, which kind of defeats the whole point.

HTH
Jul 4 '08 #8
On Jul 4, 4:19*pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
Gordon wrote:
On Jul 4, 9:50 am, Mathieu Maes <mathieu.m...@gmail.comwrote:
On 4 jul, 10:14, Gordon <gordon.mc...@ntlworld.comwrote:
>On Jul 3, 4:29 pm, Michael Fesser <neti...@gmx.dewrote:
.oO(Gordon)
I want to provide a set of static functions in a superclass that work
with class constants defined in a decendant of that class.
Unfortunately I've run into a snag with this idea. *Example:
class SuperClass
{
* *const CNST * * *= 'Super class';
* *public static function getCnst ()
* *{
* * * * * *return self::CNST;
* *}
}
class SubClass extends SuperClass
{
* *const CNST * * *= 'Sub class';
}
echo (SubClass::getCnst ());
I'd want the above code tou output "Sub Class", but instead it outputs
"Super class".
It would sem that static functions can't work with overridden
constants in subclasses, or am I missing something?
Currently static references are resolved at compile time. What you need
here is called late static binding and scheduled for PHP 5.3.
Micha
Thanks.
Is there any kind of a workaround possible in older versions? *The
only way I've found to get it to work as intended is to "inherit from
the clipboard" and cut and paste the methods into all the classes.
Maintainence nightmare.
Theoratically speaking, a constant that needs to change during
execution is not a constant.
Could you explain the context a bit more ? I don't really see what
you're trying to do in your "simple" example.
The values don't have to change during execution. *They just have to
be different for different classes.
What I need is to be able to specify some parameters in a class that
describe how it is to be dealt with. *For example for creating a new
item in the database I have a page that loads a HTML form for the user
to populate. *However, each different type of item has a different
form. *For example a document needs a field for content but a folder
doesn't as a folder is just a container for other items. *I want to
specify in a class constant attributes for things such as that. *For
example, a Folder class could have a NEW_FORM constant set to
'newfolder.php' whereas a Document class could have a NEW_FORM with a
value of 'newdoc.php'.
You have to be able to know what these values are before you can
instantise a class, however. *Of course you can do a switch statement
based on the class of the item you intend to create, but that means
that, for example, I later want to add a new Blog class, then as well
as creating the new class I also have to remember to add Blog as an
option in the switch statement. *If the Blog class itself can define
what makes it different from the other classes the system has to work
with then I can elimate the switch statement.
Of course there is only one switch statement in the above example but
in the real app there is more than one script that has to deal with
different types of object. *If I used switch statements in all of
those cases then the result would have poor maintainability.
As was stated earlier by another poster the next version of PHP will
have a solution to this problem, namely late static binding. *I guess
it's a common enough problem that they needed to add that feature to
the new release. *Of course PHP 5.3 could be days off or several
months off and was hoping to find a workaround in the meantime.

But in your example, your inheritance hierarchy is incorrect. *In folder
is not a "typeof" document. *And no, physical storage media doesn't count.

Derived classes should always be more specific examples of the base class..

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
Both Folder and Document inherit from a common ancestor, Item.
Everything in the system have some commonality, they all have a
numeric ID and numeric parent (though it can be 0 for root items, aka
Sites), they all have titles and filesystem names, and they all have
metadata for searching. There are other classes that also descend
from Item, called Template, and Asset. Folder has a descendant, Site
(A Site is a Folder that is always a root item, and when they are
created they need to have some system Folders set up). The next
edition will be a Survey class, that descends from Document. A Survey
behaves like a Document (it's basically a Document that contains a web
form in terms of presentation) but it also has to handle data
acquisition.

The idea is to define static methods in Item that work on the
constants defined in the various descendant classes. Of course until
late static binding is available that's not going to work.
Jul 5 '08 #9
On Jul 4, 7:01*pm, Chad Burrus <chad.bur...@lat-inc.comwrote:
On Jul 4, 4:14*am, Gordon <gordon.mc...@ntlworld.comwrote:
On Jul 3, 4:29 pm, Michael Fesser <neti...@gmx.dewrote:
.oO(Gordon)
I want to provide a set of static functions in a superclass that work
with class constants defined in a decendant of that class.
Unfortunately I've run into a snag with this idea. *Example:
class SuperClass
{
* *const CNST * * *= 'Super class';
* *public static function getCnst ()
* *{
* * * * * *return self::CNST;
* *}
}
class SubClass extends SuperClass
{
* *const CNST * * *= 'Sub class';
}
echo (SubClass::getCnst ());
I'd want the above code tou output "Sub Class", but instead it outputs
"Super class".
It would sem that static functions can't work with overridden
constants in subclasses, or am I missing something?
Currently static references are resolved at compile time. What you need
here is called late static binding and scheduled for PHP 5.3.
Micha
Thanks.
Is there any kind of a workaround possible in older versions? *The
only way I've found to get it to work as intended is to "inherit from
the clipboard" and cut and paste the methods into all the classes.
Maintainence nightmare.

One workaround that works with instances of a class (which
unfortunately does not work with static calls like in your example) is
to use the get_class function to grab the class name of the current
object, and then build a string you can pass to the constant function.

However, since you want static functions, the only way I know of to
work around the problem is to cheat: override the getConst function in
SubClass and pass in the class name to the SuperClass function, which
will give you something like this:

class SuperClass
{
* * * * const CNST = 'Super class';

* * * * public static function getCnst ($class = NULL)
* * * * {
* * * * * * * * if (!$class)
* * * * * * * * {
* * * * * * * * * * * * $class = get_class();
* * * * * * * * }
* * * * * * * * return constant($class . '::CNST');
* * * * }

}

class SubClass extends SuperClass
{
* * * * const CNST = 'Sub class';

* * * * public static function getCnst ()
* * * * {
* * * * * * * * return parent::getCnst(get_class());
* * * * }

}

echo SubClass::getCnst();
// outputs "Sub class"

You somewhat avoid the "inherit by clipboard" maintenance issue, but
you still have to override the getCnst method in all of your
subclasses, which kind of defeats the whole point.

HTH
Thanks, that's not a bad idea. While the functions still have to be in
all the classes only the ancestor class has to define the actual
logic, all the descendants only have to call it. That's certainly an
improvement.
Jul 5 '08 #10
Gordon wrote:
On Jul 4, 4:19 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
>Gordon wrote:
>>On Jul 4, 9:50 am, Mathieu Maes <mathieu.m...@gmail.comwrote:
On 4 jul, 10:14, Gordon <gordon.mc...@ntlworld.comwrote:
On Jul 3, 4:29 pm, Michael Fesser <neti...@gmx.dewrote:
>.oO(Gordon)
>>I want to provide a set of static functions in a superclass that work
>>with class constants defined in a decendant of that class.
>>Unfortunately I've run into a snag with this idea. Example:
>>class SuperClass
>>{
>> const CNST = 'Super class';
>> public static function getCnst ()
>> {
>> return self::CNST;
>> }
>>}
>>class SubClass extends SuperClass
>>{
>> const CNST = 'Sub class';
>>}
>>echo (SubClass::getCnst ());
>>I'd want the above code tou output "Sub Class", but instead it outputs
>>"Super class".
>>It would sem that static functions can't work with overridden
>>constants in subclasses, or am I missing something?
>Currently static references are resolved at compile time. What you need
>here is called late static binding and scheduled for PHP 5.3.
>Micha
Thanks.
Is there any kind of a workaround possible in older versions? The
only way I've found to get it to work as intended is to "inherit from
the clipboard" and cut and paste the methods into all the classes.
Maintainence nightmare.
Theoratically speaking, a constant that needs to change during
execution is not a constant.
Could you explain the context a bit more ? I don't really see what
you're trying to do in your "simple" example.
The values don't have to change during execution. They just have to
be different for different classes.
What I need is to be able to specify some parameters in a class that
describe how it is to be dealt with. For example for creating a new
item in the database I have a page that loads a HTML form for the user
to populate. However, each different type of item has a different
form. For example a document needs a field for content but a folder
doesn't as a folder is just a container for other items. I want to
specify in a class constant attributes for things such as that. For
example, a Folder class could have a NEW_FORM constant set to
'newfolder.php' whereas a Document class could have a NEW_FORM with a
value of 'newdoc.php'.
You have to be able to know what these values are before you can
instantise a class, however. Of course you can do a switch statement
based on the class of the item you intend to create, but that means
that, for example, I later want to add a new Blog class, then as well
as creating the new class I also have to remember to add Blog as an
option in the switch statement. If the Blog class itself can define
what makes it different from the other classes the system has to work
with then I can elimate the switch statement.
Of course there is only one switch statement in the above example but
in the real app there is more than one script that has to deal with
different types of object. If I used switch statements in all of
those cases then the result would have poor maintainability.
As was stated earlier by another poster the next version of PHP will
have a solution to this problem, namely late static binding. I guess
it's a common enough problem that they needed to add that feature to
the new release. Of course PHP 5.3 could be days off or several
months off and was hoping to find a workaround in the meantime.
But in your example, your inheritance hierarchy is incorrect. In folder
is not a "typeof" document. And no, physical storage media doesn't count.

Derived classes should always be more specific examples of the base class.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================

Both Folder and Document inherit from a common ancestor, Item.
Everything in the system have some commonality, they all have a
numeric ID and numeric parent (though it can be 0 for root items, aka
Sites), they all have titles and filesystem names, and they all have
metadata for searching. There are other classes that also descend
from Item, called Template, and Asset. Folder has a descendant, Site
(A Site is a Folder that is always a root item, and when they are
created they need to have some system Folders set up). The next
edition will be a Survey class, that descends from Document. A Survey
behaves like a Document (it's basically a Document that contains a web
form in terms of presentation) but it also has to handle data
acquisition.

The idea is to define static methods in Item that work on the
constants defined in the various descendant classes. Of course until
late static binding is available that's not going to work.
It sounds like you're trying to derive everything from Item - which
really isn't a good idea. It ties all of your classes more closely
togehter, making them more dependent on each other.

Also, a Site is not a "type of" folder, although it can refer to a
folder Neither is a Survey a type of a Document. The results generated
from the survey, however, could be. Again, the document would be
related to the survey.

All in all, I think your hierarchy is going to cause you a lot more
problems than you see now.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Jul 5 '08 #11
On Jul 5, 1:55*pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
Gordon wrote:
On Jul 4, 4:19 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
Gordon wrote:
On Jul 4, 9:50 am, Mathieu Maes <mathieu.m...@gmail.comwrote:
On 4 jul, 10:14, Gordon <gordon.mc...@ntlworld.comwrote:
On Jul 3, 4:29 pm, Michael Fesser <neti...@gmx.dewrote:
.oO(Gordon)
>I want to provide a set of static functions in a superclass that work
>with class constants defined in a decendant of that class.
>Unfortunately I've run into a snag with this idea. *Example:
>class SuperClass
>{
>* *const CNST * * *= 'Super class';
>* *public static function getCnst ()
>* *{
>* * * * * *return self::CNST;
>* *}
>}
>class SubClass extends SuperClass
>{
>* *const CNST * * *= 'Sub class';
>}
>echo (SubClass::getCnst ());
>I'd want the above code tou output "Sub Class", but instead it outputs
>"Super class".
>It would sem that static functions can't work with overridden
>constants in subclasses, or am I missing something?
Currently static references are resolved at compile time. What youneed
here is called late static binding and scheduled for PHP 5.3.
Micha
Thanks.
Is there any kind of a workaround possible in older versions? *The
only way I've found to get it to work as intended is to "inherit from
the clipboard" and cut and paste the methods into all the classes.
Maintainence nightmare.
Theoratically speaking, a constant that needs to change during
execution is not a constant.
Could you explain the context a bit more ? I don't really see what
you're trying to do in your "simple" example.
The values don't have to change during execution. *They just have to
be different for different classes.
What I need is to be able to specify some parameters in a class that
describe how it is to be dealt with. *For example for creating a new
item in the database I have a page that loads a HTML form for the user
to populate. *However, each different type of item has a different
form. *For example a document needs a field for content but a folder
doesn't as a folder is just a container for other items. *I want to
specify in a class constant attributes for things such as that. *For
example, a Folder class could have a NEW_FORM constant set to
'newfolder.php' whereas a Document class could have a NEW_FORM with a
value of 'newdoc.php'.
You have to be able to know what these values are before you can
instantise a class, however. *Of course you can do a switch statement
based on the class of the item you intend to create, but that means
that, for example, I later want to add a new Blog class, then as well
as creating the new class I also have to remember to add Blog as an
option in the switch statement. *If the Blog class itself can define
what makes it different from the other classes the system has to work
with then I can elimate the switch statement.
Of course there is only one switch statement in the above example but
in the real app there is more than one script that has to deal with
different types of object. *If I used switch statements in all of
those cases then the result would have poor maintainability.
As was stated earlier by another poster the next version of PHP will
have a solution to this problem, namely late static binding. *I guess
it's a common enough problem that they needed to add that feature to
the new release. *Of course PHP 5.3 could be days off or several
months off and was hoping to find a workaround in the meantime.
But in your example, your inheritance hierarchy is incorrect. *In folder
is not a "typeof" document. *And no, physical storage media doesn't count.
Derived classes should always be more specific examples of the base class.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
Both Folder and Document inherit from a common ancestor, Item.
Everything in the system have some commonality, they all have a
numeric ID and numeric parent (though it can be 0 for root items, aka
Sites), they all have titles and filesystem names, and they all have
metadata for searching. *There are other classes that also descend
from Item, called Template, and Asset. *Folder has a descendant, Site
(A Site is a Folder that is always a root item, and when they are
created they need to have some system Folders set up). The next
edition will be a Survey class, that descends from Document. A Survey
behaves like a Document (it's basically a Document that contains a web
form in terms of presentation) but it also has to handle data
acquisition.
The idea is to define static methods in Item that work on the
constants defined in the various descendant classes. *Of course until
late static binding is available that's not going to work.

It sounds like you're trying to derive everything from Item - which
really isn't a good idea. *It ties all of your classes more closely
togehter, making them more dependent on each other.

Also, a Site is not a "type of" folder, although it can refer to a
folder Neither is a Survey a type of a Document. *The results generated
from the survey, however, could be. *Again, the document would be
related to the survey.

All in all, I think your hierarchy is going to cause you a lot more
problems than you see now.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
Conceptually a Site is a type of Folder, because both are represented
in the filesystem by directories and are containers for other items.
The Site is essentially a root Folder. The reason for the distinction
is that a Site is expected to contain a set of Folders the system uses
for storing uploaded files, images, etc, so its CreateItem method
creates them as well. As for the Survey, the interface it presents to
the outside world is a HTML page with a form in it, in other words a
Document (which represents a HTML page) with content appropriate to
the task in hand of collecting data. The results collected by the
survey are a different matter as they can be viewed different ways, as
a table, bar chart, pie chart, CSV file etc, and thus aren't really
conceptually Documents.

As for the Item class it handles all the tasks common to everything
managed by the system. They all have metadata, they all abstract
things that exist on the filesystem, etc. The Item class is the
biggest class, but it's not considerably bigger than the classes that
descend from it.

And as has already been established, the problem I'm having is down to
PHP not implementing functionality that I need for it, and that said
missing functionality will be in a future version.
Jul 5 '08 #12
Gordon wrote:
On Jul 5, 1:55 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
>Gordon wrote:
>>On Jul 4, 4:19 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
Gordon wrote:
On Jul 4, 9:50 am, Mathieu Maes <mathieu.m...@gmail.comwrote:
>On 4 jul, 10:14, Gordon <gordon.mc...@ntlworld.comwrote:
>>On Jul 3, 4:29 pm, Michael Fesser <neti...@gmx.dewrote:
>>>.oO(Gordon)
>>>>I want to provide a set of static functions in a superclass that work
>>>>with class constants defined in a decendant of that class.
>>>>Unfortunately I've run into a snag with this idea. Example:
>>>>class SuperClass
>>>>{
>>>> const CNST = 'Super class';
>>>> public static function getCnst ()
>>>> {
>>>> return self::CNST;
>>>> }
>>>>}
>>>>class SubClass extends SuperClass
>>>>{
>>>> const CNST = 'Sub class';
>>>>}
>>>>echo (SubClass::getCnst ());
>>>>I'd want the above code tou output "Sub Class", but instead it outputs
>>>>"Super class".
>>>>It would sem that static functions can't work with overridden
>>>>constants in subclasses, or am I missing something?
>>>Currently static references are resolved at compile time. What you need
>>>here is called late static binding and scheduled for PHP 5.3.
>>>Micha
>>Thanks.
>>Is there any kind of a workaround possible in older versions? The
>>only way I've found to get it to work as intended is to "inherit from
>>the clipboard" and cut and paste the methods into all the classes.
>>Maintainence nightmare.
>Theoratically speaking, a constant that needs to change during
>execution is not a constant.
>Could you explain the context a bit more ? I don't really see what
>you're trying to do in your "simple" example.
The values don't have to change during execution. They just have to
be different for different classes.
What I need is to be able to specify some parameters in a class that
describe how it is to be dealt with. For example for creating a new
item in the database I have a page that loads a HTML form for the user
to populate. However, each different type of item has a different
form. For example a document needs a field for content but a folder
doesn't as a folder is just a container for other items. I want to
specify in a class constant attributes for things such as that. For
example, a Folder class could have a NEW_FORM constant set to
'newfolder.php' whereas a Document class could have a NEW_FORM with a
value of 'newdoc.php'.
You have to be able to know what these values are before you can
instantise a class, however. Of course you can do a switch statement
based on the class of the item you intend to create, but that means
that, for example, I later want to add a new Blog class, then as well
as creating the new class I also have to remember to add Blog as an
option in the switch statement. If the Blog class itself can define
what makes it different from the other classes the system has to work
with then I can elimate the switch statement.
Of course there is only one switch statement in the above example but
in the real app there is more than one script that has to deal with
different types of object. If I used switch statements in all of
those cases then the result would have poor maintainability.
As was stated earlier by another poster the next version of PHP will
have a solution to this problem, namely late static binding. I guess
it's a common enough problem that they needed to add that feature to
the new release. Of course PHP 5.3 could be days off or several
months off and was hoping to find a workaround in the meantime.
But in your example, your inheritance hierarchy is incorrect. In folder
is not a "typeof" document. And no, physical storage media doesn't count.
Derived classes should always be more specific examples of the base class.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
Both Folder and Document inherit from a common ancestor, Item.
Everything in the system have some commonality, they all have a
numeric ID and numeric parent (though it can be 0 for root items, aka
Sites), they all have titles and filesystem names, and they all have
metadata for searching. There are other classes that also descend
from Item, called Template, and Asset. Folder has a descendant, Site
(A Site is a Folder that is always a root item, and when they are
created they need to have some system Folders set up). The next
edition will be a Survey class, that descends from Document. A Survey
behaves like a Document (it's basically a Document that contains a web
form in terms of presentation) but it also has to handle data
acquisition.
The idea is to define static methods in Item that work on the
constants defined in the various descendant classes. Of course until
late static binding is available that's not going to work.
It sounds like you're trying to derive everything from Item - which
really isn't a good idea. It ties all of your classes more closely
togehter, making them more dependent on each other.

Also, a Site is not a "type of" folder, although it can refer to a
folder Neither is a Survey a type of a Document. The results generated
from the survey, however, could be. Again, the document would be
related to the survey.

All in all, I think your hierarchy is going to cause you a lot more
problems than you see now.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================

Conceptually a Site is a type of Folder, because both are represented
in the filesystem by directories and are containers for other items.
The Site is essentially a root Folder. The reason for the distinction
is that a Site is expected to contain a set of Folders the system uses
for storing uploaded files, images, etc, so its CreateItem method
creates them as well. As for the Survey, the interface it presents to
the outside world is a HTML page with a form in it, in other words a
Document (which represents a HTML page) with content appropriate to
the task in hand of collecting data. The results collected by the
survey are a different matter as they can be viewed different ways, as
a table, bar chart, pie chart, CSV file etc, and thus aren't really
conceptually Documents.
Not so. The file system is only the way the data is stored. What if
you had a site which was entirely database driven? Then you have no
"file system" - just a single generic file and a .htaccess file.

Don't mix the physical representation and the overall design. It's a
common problem.

In a true OO environment, the site will NOT be a type of folder -
conceptually they are two entirely different types of objects.

As for the Item class it handles all the tasks common to everything
managed by the system. They all have metadata, they all abstract
things that exist on the filesystem, etc. The Item class is the
biggest class, but it's not considerably bigger than the classes that
descend from it.
Again, not really valid. From your design, almost everything would be
derived from Item - which means EVERYTHING it tied together. When you
have that type of relationship, it's much harder to make changes.
And as has already been established, the problem I'm having is down to
PHP not implementing functionality that I need for it, and that said
missing functionality will be in a future version.
No, you think the problem is not implementing the functionality you
need. I think you have an invalid inheritance hierarchy. You're only
seeing the initial problem (which, BTW, will occur in virtually any OO
language). You will have others in the future.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Jul 6 '08 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ali Eghtebas | last post by:
Hi, I have 3 questions regarding the code below: 1) Why can't I trap the KEYDOWN while I can trap KEYUP? 2) Is it correct that I use Return True within the IF-Statement? (I've already read...
8
by: Edward Diener | last post by:
Is it possible for a derived class to override a property and/or event of its base class ?
5
by: Hongzheng Wang | last post by:
Hi, I have a problem about the overriding of private methods of base class. That is, if a method f() of base class is private, can the derived class overriding f() be overriding? For...
8
by: Massimiliano Alberti | last post by:
Can I specialize a template function in a subclass without overriding it? (the main template function is defined in a base class). Now I'm doing something like that: (in base class)...
15
by: Susan Baker | last post by:
Hello everybody, I'm new to C++ (I have some C background). I've read up on this topic a few times but it just dosen't seem to be sinking in. 1. Whats the difference between overloading and...
2
by: ESPNSTI | last post by:
Hi, I'm very new to C# and .Net, I've been working with it for about a month. My experience has been mainly with Delphi 5 (not .Net). What I'm looking for is for a shortcut way to override a...
17
by: Bob Weiner | last post by:
What is the purpose of hiding intead of overriding a method? I have googled the question but haven't found anything that makes any sense of it. In the code below, the only difference is that...
3
by: Amin Sobati | last post by:
Hi, I have two classes. Class2 inhertis Class1: ----------------------------- Public Class Class1 Public Overridable Sub MySub() End Sub End Class Public Class Class2
10
by: r035198x | last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize. These were designed to be overridden according to specific general contracts. Other classes that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.