473,320 Members | 2,162 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,320 software developers and data experts.

static member of a class and inheritance

I have this piece of code:
abstract class Kaos
{
static $foo;
abstract function MyFunc();
}

...
$bar = "aaa";
class Kronos extends Kaos
{
function MyFunc() {
echo self::$foo;
}
}
Kronos::$foo = $bar;

...
$bar = "bbb";
class Zeus extends Kronos
{
function MyFunc() {
parent::MyFunc();
echo self::foo;
}
}
Zeus::$foo = $bar;
At this point i would like that this code to show "aaabbb", but it
shows "bbb";

$a = new Zeus();
$a -MyFunc();

Why ? I think that i have misunderstood the meaning of 'static' keyword
for a class member, but i need a field that has the same name in each
class, but in the calls of Kronos (of a Zeus object) its value is "aaa"
and in the calls of Zeus its value is "bbb".
How can i do this ? Is possible ?

Thanks in advance
Bye

Sep 25 '06 #1
4 1245
gi*************@gmail.com said the following on 25/09/2006 17:55:
I have this piece of code:
abstract class Kaos
{
static $foo;
abstract function MyFunc();
}

..
$bar = "aaa";
class Kronos extends Kaos
{
function MyFunc() {
echo self::$foo;
}
}
Kronos::$foo = $bar;

..
$bar = "bbb";
class Zeus extends Kronos
{
function MyFunc() {
parent::MyFunc();
echo self::foo;
^
^
Where is the $ sign here??
}
}
Zeus::$foo = $bar;
At this point i would like that this code to show "aaabbb", but it
shows "bbb";

$a = new Zeus();
$a -MyFunc();

Why ? I think that i have misunderstood the meaning of 'static' keyword
for a class member, but i need a field that has the same name in each
class, but in the calls of Kronos (of a Zeus object) its value is "aaa"
and in the calls of Zeus its value is "bbb".
How can i do this ? Is possible ?
Not this way. There is only instance of $foo, contained as a static
member of Kaos. The behaviour you are getting is similar to that in C++
and Java.

You could have a separate static $foo declared in each class; however,
this too has limitations.

What exactly do you need this for? i.e. how are you using $foo? There
may be a more suitable solution to your problem.

--
Oli
Sep 25 '06 #2
[cut]
echo self::foo;
^
^
Where is the $ sign here??
My fault.. it's self::$foo of course

[cut]
>
Not this way. There is only instance of $foo, contained as a static
member of Kaos. The behaviour you are getting is similar to that in C++
and Java.

You could have a separate static $foo declared in each class; however,
this too has limitations.

What exactly do you need this for? i.e. how are you using $foo? There
may be a more suitable solution to your problem.

That's what i have:
there a lot of objects, and each one is located in its own directory
according to this convention:
class file path = 'obj/{$className}/class.{$className}.php'
and i declared the __autoload() function to load any object when
needed.
so i have one function that loads any object, but every object needs to
know where it is located to correctly link its own resources file
(located in its own directory), so i thought to do something like this:

any object extends this:

abstract class Obj
{
static $objpath;
... /* other */ ..
}

function __autoload($classname)
{
$objpath = 'obj/{$classname}/class.{$classname}.php';
require_once( $objpath );
eval( $classname.'::$objpath = $objpath' );
}

so i would like that any object knows where it is, and it knows it by
reading self::$objpath. But i fell in this inconvenience when a parent
object used the directory of its child to take its resource file.

So, the right way is to declare a 'static $objpath' in each class ?
perhaps something more elegant ?
Giacomo

Sep 25 '06 #3
gi*************@gmail.com said the following on 25/09/2006 21:06:
That's what i have:
there a lot of objects, and each one is located in its own directory
according to this convention:
class file path = 'obj/{$className}/class.{$className}.php'
and i declared the __autoload() function to load any object when
needed.
so i have one function that loads any object, but every object needs to
know where it is located to correctly link its own resources file
(located in its own directory), so i thought to do something like this:

any object extends this:

abstract class Obj
{
static $objpath;
... /* other */ ..
}

function __autoload($classname)
{
$objpath = 'obj/{$classname}/class.{$classname}.php';
require_once( $objpath );
eval( $classname.'::$objpath = $objpath' );
}

so i would like that any object knows where it is, and it knows it by
reading self::$objpath. But i fell in this inconvenience when a parent
object used the directory of its child to take its resource file.

So, the right way is to declare a 'static $objpath' in each class ?
perhaps something more elegant ?
You can get the directory of the class script from inside the class
using dirname(__FILE__).

i.e.:

== obj/foo/class.foo.php ==

<?php

class Foo
{
function bar()
{
// Load resources
file_get_contents(dirname(__FILE__) . "/stuff.txt");
}
}

?>

This will load the file obj/foo/stuff.txt, and does not matter when or
from where Foo::bar() is called.

--
Oli
Sep 25 '06 #4
You can get the directory of the class script from inside the class
using dirname(__FILE__).

i.e.:

== obj/foo/class.foo.php ==

<?php

class Foo
{
function bar()
{
// Load resources
file_get_contents(dirname(__FILE__) . "/stuff.txt");
}
}

?>

This will load the file obj/foo/stuff.txt, and does not matter when or
from where Foo::bar() is called.
yes, it's easier and simple at the same time.. thank you
Bye

Sep 25 '06 #5

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

Similar topics

3
by: exits funnel | last post by:
Hello, One of the problems at the end of Chapter 14 in Bruce Eckel's thinking in C++ reads as follows: Create a class with two static member functions. Inherit from this class and redefine...
5
by: Thomas Matthews | last post by:
Hi, I have three classes: Category Author Publisher Each of these classes stores its information into a database table of <ID, text>. (They are fields that have a foreign key.) There is...
10
by: cppaddict | last post by:
Hi, Say I have a abstract base class and I know that every derived class will have a static data member (call it SharedInformation) representing information to be shared across all instances of...
8
by: Srini | last post by:
Hello all, I was just wondering about this. A const member function guarantees constness of the object within the function body. But there's no way for a member function to guarantee the...
13
by: Adam H. Peterson | last post by:
I just made an observation and I wondered if it's generally known (or if I'm missing something). My observation is that static protected members are essentially useless, only a hint to the user. ...
7
by: Sunny | last post by:
Hi all, According C# Language Specification : 10.11 Static constructors: The static constructor for a class executes at most once in a given application domain. The execution of a static...
5
by: Dale | last post by:
Is it possible to declare a method of an interface as static? For instance, can I create an interface that defines a static method and 2 instance methods?
6
by: Bill Rubin | last post by:
The following code snippet shows that VC++ 7.1 correctly compiles a static member function invocation from an Unrelated class, since this static member function is public. I expected to compile the...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
0
by: Axter | last post by:
I'm currently working on the following policy base smart pointer: http://code.axter.com/smart_ptr.h Before working on the above code, I read the following links:...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.