Re: RE:Class::? - accessing data, no success...
| From: | Kristian Köhntopp | Date: | Wed, 05 Jul 2000 13:43:04 +0000 |
| Subject: | Re: RE:Class::? - accessing data, no success... | ||
| References: | 1 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-23572@lists.php.net to get a copy of this message | ||
Stanislav Malyshev wrote:
> KK>> The class C could have some class variables c1, c2, c3.
>
> What is "class variable"? How is it different from globals?
In scope. A global uses up a name in the global namespace. A
class variable uses up a name in the class namespace. If you
think of classes as instances of a class Metaclass, this will
be exactly defined.
> KK>> $C::c1 = "something";
> KK>> $C::c2 = 17;
>
> Does this change it in all instances of class C? If so, I fear there's no
> good way to do it in PHP.
Think of an object $o with instance variables $o->o1, $o->o2,
$o->o3, $o being an instance of class C as a hash of the
following form:
$o["o1"] = 1;
$o["o2"] = 2;
$o["o3"] = 3;
$o["isa"] = &$c; // reference to Class C.
$o also has the instance methods $o->im1(), $o->im2(), and
$o->im3().
Think of a class C as a hash of the following form:
$c["name"] = "C";
$c["im1"] = &im1(); // reference to $o->im1();
$c["im2"] = &im2();
$c["im3"] = &im3();
$c["isa"] = &$m; // reference to Class Metaclass (here called m).
The class contains pointers to all methods available to
instances of C. It can also contain additional pointers
to functions not available to instances, but only to the
class itself, and to variables not available to instances,
but only to the class itself. If you change these variables,
they will change for the class, or, as you say, for "all instances",
as they are not kept in the instance, but in the class.
$c["cm1"] = &cm1(); // marked specially so that this can be
// distinguished from im1. This is a class method.
$c["c1"] = 1; // marked specially, so that this can be
// recognized as a class variable.
Then there is the class Metaclass, here called $m. This contains
all instance methods available to class objects such as $c.
Such methods would be get_class(), get_parent_class(),
or new().
Again, have a look at the Smalltalk model, or at Objective-C.
They explain it much better than I do.
http://www.amazon.com/exec/obidos/ASIN/0201136880
"Smalltalk 80: The language"
http://developer.apple.com/techpubs/macosx/macosx.html
"MacOS X Developer Documentation"
http://developer.apple.com/techpubs/macosx/System/Documentation/Developer/Cocoa/ObjectiveC/ObjC.pdf
"Objective-C Language"
> KK>> $C::cm1();
> KK>> ...
> KK>>
> KK>> to call class methods, if possible.
>
> No. $var() means "take name from var, and call method by this name".
Which this does.
> Using such a method looks like a bad hack to me. Why to have a class if
> you don't instantiate it? Just to call it a "class"? Call it "utility
> library" and leave classes alone.
This is not what this is about. There may be instances of
class. Class variables can use useful to track instances
of the class. I gave examples in a previous post, naming
a database connection class like DB_Sql, keeping references
to all open database handles in a class variable and a class
Drawable, keeping references to all drawable instances in a
class variables. Class function would then be useful to
invoke actions on all instances, or to ask for values of
class variables in a functional manner (accessor functions).
I also stated that you could manually code a C_Keeper for
each class C, create a single instance of this and use this
to emulate class variables and class methods of C as instance
variables and instance methods of C_Keeper. This is slightly
boring, but a functioning workaround.
> KK>> useful, as there will be less references to the hardcoded
> KK>> classname in the code - the class name should be used only
> KK>> in one place, the class definition "class <name>" or
> KK>> "class <name> extends <name'>".
>
> I know a thing less then 1. It's 0.
???
Kristian
--
Kristian Köhntopp, NetUSE AG Siemenswall, D-24107 Kiel
Tel: +49 431 386 436 00, Fax: +49 431 386 435 99
Using PHP3? See our web development library at http://phplib.netuse.de/