|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Can anyone tell me the difference between the following two lines:
CLASS::function(); CLASS->function(); |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
> CLASS::function();
Calls a "class method". It is like a function in the class namespace. No instance is needed. > CLASS->function(); Make that $INSTANCE->function(); This calls a method on an object instance. You will have to create one first with $INSTANCE = new SomeClassName(); Regards, -- Willem Bogaerts Application smith Kratz B.V. http://www.kratz.nl/ |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
I see, thanks for that - what is the avantage / disadvantage of each?
On Feb 15, 1:27 pm, Willem Bogaerts <w.bogae...@kratz.maardanzonderditstuk.nl> wrote: > > CLASS::function(); > > Calls a "class method". It is like a function in the class namespace. No > instance is needed. > > > CLASS->function(); > > Make that $INSTANCE->function(); > This calls a method on an object instance. You will have to create one > first with $INSTANCE = new SomeClassName(); > > Regards, > -- > Willem Bogaerts > > Application smith > Kratz B.V.http://www.kratz.nl/ |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
"Joe" <joe@faceh.com> wrote in message news:49bf973c-4b29-4a70-9dca-8ffeda3ea77c@e6g2000prf.googlegroups.com... >I see, thanks for that - what is the avantage / disadvantage of each? please quit top-posting. the 'class::method not requiring an instance' is very misleading...as 'class' will point to an instance every time...except when calling static interfaces. 'class' is usually self::method() or parent::method() etc. this is not a dis/advantage scenario. it is a necessity because a caller knows the instance being created... $object = new someClass(); as a 'caller', the instance i'm going to be using is '$object'. as an object/class developer, i may extend an existing class. i have no specific way of referring to my 'parent' when i'm being used except via the language construct parent::method(). the same is almost true for self::method. php still has the javascript-ish construct of using $this->method() for referring to the current class. i prefer self::method() but both are usable. as for a 'caller' not creating an instance of an object before calling an interface...i.e. someClass->method(); it is doable in php < 5, however afaik, php is going to start throwing warnings in php 5 for this...and fatal errors in php 6. again...please DON'T top-post. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
First of all, sorry for "top-post"ing, but I don't even know what that
means... Secondly, thanks for your reply, but I still don't entirely understand to be honest. Just to clarify: 1) Within the same class, for all intensive purposes self::function() and $this->function() are the same, is that correct? However self::function() is the way PHP5 is meant to be used properly? 2) parent::function() is used for accessing the functions of a parent class from an extension of that parent class, right? In writing that I think I understand a little better. I am so used to just using -> for everything. Also, I dont extend objects so often so I guess I have little use for it yet. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
Joe wrote:
> First of all, sorry for "top-post"ing, but I don't even know what that > means... http://en.wikipedia.org/wiki/Top-posting -- ---------------------------------- Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org- MSN:i_eat_s_p_a_m_for_breakfast@hotmail.com Jabber:ivansanchez@jabber.org ; ivansanchez@kdetalk.net |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
Joe wrote:
> First of all, sorry for "top-post"ing, but I don't even know what that > means... > > Secondly, thanks for your reply, but I still don't entirely understand > to be honest. Just to clarify: > > 1) Within the same class, for all intensive purposes self::function() > and $this->function() are the same, is that correct? However > self::function() is the way PHP5 is meant to be used properly? > > 2) parent::function() is used for accessing the functions of a parent > class from an extension of that parent class, right? > > In writing that I think I understand a little better. I am so used to > just using -> for everything. Also, I dont extend objects so often so > I guess I have little use for it yet. > Steve is wrong in his assertions. CLASS::function does not point to an instance of an object when called from outside the class. That means: 1. The function being called must be a static function 2. The function being called can only access static members of the class (including functions) and class constants - not non-static variables or functions $INSTANCE->function() requires you first allocate an object of the class. It can: 1. Call any function in the class, subject to private/protected restrictions 2. The function called can access any members of the class. Remember that for static variables, all instances of the class share one value. Non-static variables will have separate values for each instance. "self" and "parent" are special keywords to refer to the class itself and it's parent class, respectively. Despite the similarity in syntax, they are not the same. They can only be used within class functions. But within a class function, CLASS::function() and self::function() are similar. Now I should also mention that PHP does not enforce all of these rules. However, I wouldn't depend on that remaining the same, either. At some future time it may be that it tightens down the restrictions and makes them similar to other OO languages such as C++ and Java. In that case, self::function() would not be the same as CLASS::function(), even within a class member function. -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstucklex@attglobal.net ================== |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
"Jerry Stuckle" <jstucklex@attglobal.net> wrote in message news:Ed2dnQHX7a3pLyjanZ2dnUVZ_vCknZ2d@comcast.com. .. > Joe wrote: >> First of all, sorry for "top-post"ing, but I don't even know what that >> means... >> >> Secondly, thanks for your reply, but I still don't entirely understand >> to be honest. Just to clarify: >> >> 1) Within the same class, for all intensive purposes self::function() >> and $this->function() are the same, is that correct? However >> self::function() is the way PHP5 is meant to be used properly? >> >> 2) parent::function() is used for accessing the functions of a parent >> class from an extension of that parent class, right? >> >> In writing that I think I understand a little better. I am so used to >> just using -> for everything. Also, I dont extend objects so often so >> I guess I have little use for it yet. >> > > Steve is wrong in his assertions. jerry, please list my assertions and next to each, tell what is wrong about it. your first point, below, isn't what i've said at all...makes me suspicious of what you think you've read. ![]() > CLASS::function does not point to an instance of an object when called > from outside the class. That means: > > 1. The function being called must be a static function i did not claim that class::function() could NOT be called external to a class...and i quote: "...except when calling static interfaces" which implies that you CAN. i merely gave an exaple of :: usage internal to a class. that in no way likens me to saying other uses are not available. > 2. The function being called can only access static members of the class > (including functions) and class constants - not non-static variables or > functions > $INSTANCE->function() requires you first allocate an object of the class. > It can: > > 1. Call any function in the class, subject to private/protected > restrictions > 2. The function called can access any members of the class. no argument there. > Remember that for static variables, all instances of the class share one > value. Non-static variables will have separate values for each instance. > > "self" and "parent" are special keywords to refer to the class itself and > it's parent class, respectively. Despite the similarity in syntax, they > are not the same. They can only be used within class functions. But > within a class function, CLASS::function() and self::function() are > similar. yet not the same. > Now I should also mention that PHP does not enforce all of these rules. > However, I wouldn't depend on that remaining the same, either. At some > future time it may be that it tightens down the restrictions and makes > them similar to other OO languages such as C++ and Java. In that case, > self::function() would not be the same as CLASS::function(), even within a > class member function. glad you noticed. |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
"Joe" <joe@faceh.com> wrote in message news:e3d40869-5b0d-4c9c-8674-c3ed4ac4ad81@d21g2000prf.googlegroups.com... > First of all, sorry for "top-post"ing, but I don't even know what that > means... > > Secondly, thanks for your reply, but I still don't entirely understand > to be honest. Just to clarify: > > 1) Within the same class, for all intensive purposes self::function() > and $this->function() are the same, is that correct? However > self::function() is the way PHP5 is meant to be used properly? yes...and yes. > 2) parent::function() is used for accessing the functions of a parent > class from an extension of that parent class, right? yes...the class being extended. > In writing that I think I understand a little better. I am so used to > just using -> for everything. Also, I dont extend objects so often so > I guess I have little use for it yet. everything else except static object interfaces...in which case, you'd use class::function()...or class::$varOrConstant. for extending objects, you typically won't see a need to unless you are analysing your code for patterns and refactoring, *or* working from an OO designed drawing board from the onset of development (which is the best case scenario). either way, it's there for you to you if you need to...understanding how it works will you know when. cheers. |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
"Steve" <no.one@example.com> wrote in message news:Tljtj.21$Lq2.0@newsfe05.lga... > > "Jerry Stuckle" <jstucklex@attglobal.net> wrote in message > news:Ed2dnQHX7a3pLyjanZ2dnUVZ_vCknZ2d@comcast.com. .. >> Joe wrote: >>> First of all, sorry for "top-post"ing, but I don't even know what that >>> means... >>> >>> Secondly, thanks for your reply, but I still don't entirely understand >>> to be honest. Just to clarify: >>> >>> 1) Within the same class, for all intensive purposes self::function() >>> and $this->function() are the same, is that correct? However >>> self::function() is the way PHP5 is meant to be used properly? >>> >>> 2) parent::function() is used for accessing the functions of a parent >>> class from an extension of that parent class, right? >>> >>> In writing that I think I understand a little better. I am so used to >>> just using -> for everything. Also, I dont extend objects so often so >>> I guess I have little use for it yet. >>> >> >> Steve is wrong in his assertions. > > jerry, please list my assertions and next to each, tell what is wrong > about it. your first point, below, isn't what i've said at all...makes me > suspicious of what you think you've read. ![]() > >> CLASS::function does not point to an instance of an object when called >> from outside the class. That means: to be absolutely clear, 'class' in class::function *always* points to an object instance...be it self, parent, or a globally consumed object instanciated upon the first call to any of its static members. your 'correction' to my 'wrong' assertion is...well...incorrect. ![]() |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
On Feb 15, 11:54 am, "Steve" <no....@example.com> wrote:
> > to be absolutely clear, 'class' in class::function *always* points to an > object instance...be it self, parent, or a globally consumed object > instanciated upon the first call to any of its static members. > > your 'correction' to my 'wrong' assertion is...well...incorrect. ![]() Given the following code: class Foo { public static function bar() { return 42; } } $x = Foo::bar(); What instance of Foo does that last line refer to? |
|
|
|
#12 |
|
Messages: n/a
Hébergeur: |
"ZeldorBlat" <zeldorblat@gmail.com> wrote in message news:369d306f-a9b7-4428-ad57-a024b01c7832@s19g2000prg.googlegroups.com... > On Feb 15, 11:54 am, "Steve" <no....@example.com> wrote: >> >> to be absolutely clear, 'class' in class::function *always* points to an >> object instance...be it self, parent, or a globally consumed object >> instanciated upon the first call to any of its static members. >> >> your 'correction' to my 'wrong' assertion is...well...incorrect. ![]() > > Given the following code: > > class Foo { > public static function bar() { > return 42; > } > } > > $x = Foo::bar(); > > What instance of Foo does that last line refer to? a global instance that was instanciated upon the call to bar(). if you called another method, or even bar() again, no new instance would be created since a global one is now already available (and will be used for the any other calls). that's not just how it works in php, that's how it works in most other OO languages too. |
|
|
|
#13 |
|
Messages: n/a
Hébergeur: |
On Feb 15, 12:16 pm, "Steve" <no....@example.com> wrote:
> "ZeldorBlat" <zeldorb...@gmail.com> wrote in message > > news:369d306f-a9b7-4428-ad57-a024b01c7832@s19g2000prg.googlegroups.com... > > > > > On Feb 15, 11:54 am, "Steve" <no....@example.com> wrote: > > >> to be absolutely clear, 'class' in class::function *always* points to an > >> object instance...be it self, parent, or a globally consumed object > >> instanciated upon the first call to any of its static members. > > >> your 'correction' to my 'wrong' assertion is...well...incorrect. ![]() > > > Given the following code: > > > class Foo { > > public static function bar() { > > return 42; > > } > > } > > > $x = Foo::bar(); > > > What instance of Foo does that last line refer to? > > a global instance that was instanciated upon the call to bar(). if you > called another method, or even bar() again, no new instance would be created > since a global one is now already available (and will be used for the any > other calls). > > that's not just how it works in php, that's how it works in most other OO > languages too. It's not an instance of the class -- that's the distinction between class (static) methods and instance methods. If it were a "global instance" you would be able to access the instance variables (or $this, for that matter) for that one global instance, but we all know that this doesn't work: class Foo { protected $baz = 42; public static function bar() { return $this->baz; } } $x = Foo::bar(); In some languages (Smalltalk, for example) the class itself is also an object, so class methods are actually instance methods for the object that represents the class -- but that's not the case in PHP. |
|
|
|
#14 |
|
Messages: n/a
Hébergeur: |
Great, thanks for your . I have a better understanding now, and
will use this for future projects. Thanks again. |
|
|
|
#15 |
|
Messages: n/a
Hébergeur: |
"ZeldorBlat" <zeldorblat@gmail.com> wrote in message news:cfdcb50f-c98e-47f1-a54a-410855a09402@s12g2000prg.googlegroups.com... > On Feb 15, 12:16 pm, "Steve" <no....@example.com> wrote: >> "ZeldorBlat" <zeldorb...@gmail.com> wrote in message >> >> news:369d306f-a9b7-4428-ad57-a024b01c7832@s19g2000prg.googlegroups.com... >> >> >> >> > On Feb 15, 11:54 am, "Steve" <no....@example.com> wrote: >> >> >> to be absolutely clear, 'class' in class::function *always* points to >> >> an >> >> object instance...be it self, parent, or a globally consumed object >> >> instanciated upon the first call to any of its static members. >> >> >> your 'correction' to my 'wrong' assertion is...well...incorrect. ![]() >> >> > Given the following code: >> >> > class Foo { >> > public static function bar() { >> > return 42; >> > } >> > } >> >> > $x = Foo::bar(); >> >> > What instance of Foo does that last line refer to? >> >> a global instance that was instanciated upon the call to bar(). if you >> called another method, or even bar() again, no new instance would be >> created >> since a global one is now already available (and will be used for the any >> other calls). >> >> that's not just how it works in php, that's how it works in most other OO >> languages too. > > It's not an instance of the class -- that's the distinction between > class (static) methods and instance methods. If it were a "global > instance" you would be able to access the instance variables (or > $this, for that matter) for that one global instance, but we all know > that this doesn't work: sorry, Foo is a global instance. the rest of your semantics is bollocks. drop two static interfaces, initialize() and getInstance(), into Foo...where $this is returned from getInstance (google for the code, i'm not wasting the time). then do this: Foo::initialize(); // global instance now available $instance = Foo::getInstance(); // wow, there really is an instance! echo $instance->instanceVariable; // holy shit, i just did what you said i couldn't |
|
|
|
#16 |
|
Messages: n/a
Hébergeur: |
On Feb 15, 12:40 pm, "Steve" <no....@example.com> wrote:
> "ZeldorBlat" <zeldorb...@gmail.com> wrote in message > > news:cfdcb50f-c98e-47f1-a54a-410855a09402@s12g2000prg.googlegroups.com... > > > > > On Feb 15, 12:16 pm, "Steve" <no....@example.com> wrote: > >> "ZeldorBlat" <zeldorb...@gmail.com> wrote in message > > >>news:369d306f-a9b7-4428-ad57-a024b01c7832@s19g2000prg.googlegroups.com... > > >> > On Feb 15, 11:54 am, "Steve" <no....@example.com> wrote: > > >> >> to be absolutely clear, 'class' in class::function *always* points to > >> >> an > >> >> object instance...be it self, parent, or a globally consumed object > >> >> instanciated upon the first call to any of its static members. > > >> >> your 'correction' to my 'wrong' assertion is...well...incorrect. ![]() > > >> > Given the following code: > > >> > class Foo { > >> > public static function bar() { > >> > return 42; > >> > } > >> > } > > >> > $x = Foo::bar(); > > >> > What instance of Foo does that last line refer to? > > >> a global instance that was instanciated upon the call to bar(). if you > >> called another method, or even bar() again, no new instance would be > >> created > >> since a global one is now already available (and will be used for the any > >> other calls). > > >> that's not just how it works in php, that's how it works in most other OO > >> languages too. > > > It's not an instance of the class -- that's the distinction between > > class (static) methods and instance methods. If it were a "global > > instance" you would be able to access the instance variables (or > > $this, for that matter) for that one global instance, but we all know > > that this doesn't work: > > sorry, Foo is a global instance. the rest of your semantics is bollocks. > > drop two static interfaces, initialize() and getInstance(), into Foo...where > $this is returned from getInstance (google for the code, i'm not wasting the > time). then do this: > > Foo::initialize(); // global instance now available > $instance = Foo::getInstance(); // wow, there really is an instance! > echo $instance->instanceVariable; // holy shit, i just did what you said i > couldn't class Foo { public static function initialize() { return; } public static function getInstance() { return $this; } } Foo::initialize(); $obj = Foo::getInstance(); This code doesn't work because $this is undefined inside getInstance(). It's undefined because there is no instance to refer to inside that method. |
|
|
|
#17 |
|
Messages: n/a
Hébergeur: |
"ZeldorBlat" <zeldorblat@gmail.com> wrote in message news:29b90f3b-75c5-41c6-af2a-7a937e7ca028@d4g2000prg.googlegroups.com... > On Feb 15, 12:40 pm, "Steve" <no....@example.com> wrote: >> "ZeldorBlat" <zeldorb...@gmail.com> wrote in message >> >> news:cfdcb50f-c98e-47f1-a54a-410855a09402@s12g2000prg.googlegroups.com... >> >> >> >> > On Feb 15, 12:16 pm, "Steve" <no....@example.com> wrote: >> >> "ZeldorBlat" <zeldorb...@gmail.com> wrote in message >> >> >>news:369d306f-a9b7-4428-ad57-a024b01c7832@s19g2000prg.googlegroups.com... >> >> >> > On Feb 15, 11:54 am, "Steve" <no....@example.com> wrote: >> >> >> >> to be absolutely clear, 'class' in class::function *always* points >> >> >> to >> >> >> an >> >> >> object instance...be it self, parent, or a globally consumed object >> >> >> instanciated upon the first call to any of its static members. >> >> >> >> your 'correction' to my 'wrong' assertion is...well...incorrect. ![]() >> >> >> > Given the following code: >> >> >> > class Foo { >> >> > public static function bar() { >> >> > return 42; >> >> > } >> >> > } >> >> >> > $x = Foo::bar(); >> >> >> > What instance of Foo does that last line refer to? >> >> >> a global instance that was instanciated upon the call to bar(). if you >> >> called another method, or even bar() again, no new instance would be >> >> created >> >> since a global one is now already available (and will be used for the >> >> any >> >> other calls). >> >> >> that's not just how it works in php, that's how it works in most other >> >> OO >> >> languages too. >> >> > It's not an instance of the class -- that's the distinction between >> > class (static) methods and instance methods. If it were a "global >> > instance" you would be able to access the instance variables (or >> > $this, for that matter) for that one global instance, but we all know >> > that this doesn't work: >> >> sorry, Foo is a global instance. the rest of your semantics is bollocks. >> >> drop two static interfaces, initialize() and getInstance(), into >> Foo...where >> $this is returned from getInstance (google for the code, i'm not wasting >> the >> time). then do this: >> >> Foo::initialize(); // global instance now available >> $instance = Foo::getInstance(); // wow, there really is an instance! >> echo $instance->instanceVariable; // holy shit, i just did what you said >> i >> couldn't > > class Foo { > > public static function initialize() { > return; > } > > public static function getInstance() { > return $this; > } > > } > > Foo::initialize(); > $obj = Foo::getInstance(); > > This code doesn't work because $this is undefined inside > getInstance(). It's undefined because there is no instance to refer > to inside that method. ok, bad example...i was recalling the commonly used getInstance where a public instance is created and set within the class as a variable...which is returned from the function - i just recalled it badly. however and none-the-less...self and parent, though not accessible external to the object are still class-wide, singleton instances of Foo...otherwise, other singletons within the same object would not be able to share or reference other interfaces of the class (variables, functions, etc.). meaning, the variable $instance would always be null when calling getInstance...when in reality, it will only ever be null one time. class foo { private $bar = 'hello world'; private static $instance = null; public static function getInstance() { if (is_null(self::$instance)){ self::$instance = new foo(); } return self::$instance; } public function pah(){ return $this->bar; } } $foo = foo::getInstance(); echo '<pre style="font:10px;">' . $foo->pah() . '</pre>'; my primary oversight leading to the bad example was being able to declare an entire class as static in other languages. as it is in php, you can't static function __construct() et. al. even so, Foo is always an instance...either self, parent, or $instance. the interface definitions determine whether or not (and how) that instance is consumed (internal and external)...not whether an instance itself exists. when i say 'instance' it is from a php perspective and not from the caller's instance-type notation, i.e. $instace->method() or static-type notation, i.e. foo::bar(). |
|
|
|
#18 |
|
Messages: n/a
Hébergeur: |
On Feb 15, 1:47 pm, "Steve" <no....@example.com> wrote:
> "ZeldorBlat" <zeldorb...@gmail.com> wrote in message > > news:29b90f3b-75c5-41c6-af2a-7a937e7ca028@d4g2000prg.googlegroups.com... > > > > > On Feb 15, 12:40 pm, "Steve" <no....@example.com> wrote: > >> "ZeldorBlat" <zeldorb...@gmail.com> wrote in message > > >>news:cfdcb50f-c98e-47f1-a54a-410855a09402@s12g2000prg.googlegroups.com... > > >> > On Feb 15, 12:16 pm, "Steve" <no....@example.com> wrote: > >> >> "ZeldorBlat" <zeldorb...@gmail.com> wrote in message > > >> >>news:369d306f-a9b7-4428-ad57-a024b01c7832@s19g2000prg.googlegroups.com... > > >> >> > On Feb 15, 11:54 am, "Steve" <no....@example.com> wrote: > > >> >> >> to be absolutely clear, 'class' in class::function *always* points > >> >> >> to > >> >> >> an > >> >> >> object instance...be it self, parent, or a globally consumed object > >> >> >> instanciated upon the first call to any of its static members. > > >> >> >> your 'correction' to my 'wrong' assertion is...well...incorrect. ![]() > > >> >> > Given the following code: > > >> >> > class Foo { > >> >> > public static function bar() { > >> >> > return 42; > >> >> > } > >> >> > } > > >> >> > $x = Foo::bar(); > > >> >> > What instance of Foo does that last line refer to? > > >> >> a global instance that was instanciated upon the call to bar(). if you > >> >> called another method, or even bar() again, no new instance would be > >> >> created > >> >> since a global one is now already available (and will be used for the > >> >> any > >> >> other calls). > > >> >> that's not just how it works in php, that's how it works in most other > >> >> OO > >> >> languages too. > > >> > It's not an instance of the class -- that's the distinction between > >> > class (static) methods and instance methods. If it were a "global > >> > instance" you would be able to access the instance variables (or > >> > $this, for that matter) for that one global instance, but we all know > >> > that this doesn't work: > > >> sorry, Foo is a global instance. the rest of your semantics is bollocks. > > >> drop two static interfaces, initialize() and getInstance(), into > >> Foo...where > >> $this is returned from getInstance (google for the code, i'm not wasting > >> the > >> time). then do this: > > >> Foo::initialize(); // global instance now available > >> $instance = Foo::getInstance(); // wow, there really is an instance! > >> echo $instance->instanceVariable; // holy shit, i just did what you said > >> i > >> couldn't > > > class Foo { > > > public static function initialize() { > > return; > > } > > > public static function getInstance() { > > return $this; > > } > > > } > > > Foo::initialize(); > > $obj = Foo::getInstance(); > > > This code doesn't work because $this is undefined inside > > getInstance(). It's undefined because there is no instance to refer > > to inside that method. > > ok, bad example...i was recalling the commonly used getInstance where a > public instance is created and set within the class as a variable...which is > returned from the function - i just recalled it badly. however and > none-the-less...self and parent, though not accessible external to the > object are still class-wide, singleton instances of Foo...otherwise, other > singletons within the same object would not be able to share or reference > other interfaces of the class (variables, functions, etc.). meaning, the > variable $instance would always be null when calling getInstance...when in > reality, it will only ever be null one time. > > class foo > { > private $bar = 'hello world'; > private static $instance = null; > public static function getInstance() > { > if (is_null(self::$instance)){ self::$instance = new foo(); } > return self::$instance; > } > public function pah(){ return $this->bar; }} > > $foo = foo::getInstance(); > echo '<pre style="font:10px;">' . $foo->pah() . '</pre>'; > > my primary oversight leading to the bad example was being able to declare an > entire class as static in other languages. as it is in php, you can't static > function __construct() et. al. > > even so, Foo is always an instance...either self, parent, or $instance. the > interface definitions determine whether or not (and how) that instance is > consumed (internal and external)...not whether an instance itself exists. > when i say 'instance' it is from a php perspective and not from the caller's > instance-type notation, i.e. $instace->method() or static-type notation, > i.e. foo::bar(). So, if I'm reading that right, you're saying that "self" and "parent" always refer to the same instance of the class, regardless of what instance they're used from. Consider the following code: class Foo { public $x; public function getX() { return $this->x; } } class Bar extends Foo { public function __construct() { $this->x = 42; } public function getX() { return parent::getX(); } } class Baz extends Foo { public function __construct() { $this->x = 69; } public function getX() { return parent::getX(); } } $bar = new Bar(); $baz = new Baz(); $y = $bar->getX(); $z = $baz->getX(); If parent referred to the same instance no matter where it's used then $bar->getX() and $baz->getX() should return the same thing. Since they don't we know that parent must be referring to the instance from which it is used. |
|
|
|
#19 |
|
Messages: n/a
Hébergeur: |
"ZeldorBlat" <zeldorblat@gmail.com> wrote in message news:13f378bc-337b-4816-8afd-1bb81668df89@s12g2000prg.googlegroups.com... > On Feb 15, 1:47 pm, "Steve" <no....@example.com> wrote: >> "ZeldorBlat" <zeldorb...@gmail.com> wrote in message >> >> news:29b90f3b-75c5-41c6-af2a-7a937e7ca028@d4g2000prg.googlegroups.com... >> >> >> >> > On Feb 15, 12:40 pm, "Steve" <no....@example.com> wrote: >> >> "ZeldorBlat" <zeldorb...@gmail.com> wrote in message >> >> >>news:cfdcb50f-c98e-47f1-a54a-410855a09402@s12g2000prg.googlegroups.com... >> >> >> > On Feb 15, 12:16 pm, "Steve" <no....@example.com> wrote: >> >> >> "ZeldorBlat" <zeldorb...@gmail.com> wrote in message >> >> >> >>news:369d306f-a9b7-4428-ad57-a024b01c7832@s19g2000prg.googlegroups.com... >> >> >> >> > On Feb 15, 11:54 am, "Steve" <no....@example.com> wrote: >> >> >> >> >> to be absolutely clear, 'class' in class::function *always* >> >> >> >> points >> >> >> >> to >> >> >> >> an >> >> >> >> object instance...be it self, parent, or a globally consumed >> >> >> >> object >> >> >> >> instanciated upon the first call to any of its static members. >> >> >> >> >> your 'correction' to my 'wrong' assertion is...well...incorrect. >> >> >> >> ![]() >> >> >> >> > Given the following code: >> >> >> >> > class Foo { >> >> >> > public static function bar() { >> >> >> > return 42; >> >> >> > } >> >> >> > } >> >> >> >> > $x = Foo::bar(); >> >> >> >> > What instance of Foo does that last line refer to? >> >> >> >> a global instance that was instanciated upon the call to bar(). if >> >> >> you >> >> >> called another method, or even bar() again, no new instance would >> >> >> be >> >> >> created >> >> >> since a global one is now already available (and will be used for >> >> >> the >> >> >> any >> >> >> other calls). >> >> >> >> that's not just how it works in php, that's how it works in most >> >> >> other >> >> >> OO >> >> >> languages too. >> >> >> > It's not an instance of the class -- that's the distinction between >> >> > class (static) methods and instance methods. If it were a "global >> >> > instance" you would be able to access the instance variables (or >> >> > $this, for that matter) for that one global instance, but we all >> >> > know >> >> > that this doesn't work: >> >> >> sorry, Foo is a global instance. the rest of your semantics is >> >> bollocks. >> >> >> drop two static interfaces, initialize() and getInstance(), into >> >> Foo...where >> >> $this is returned from getInstance (google for the code, i'm not >> >> wasting >> >> the >> >> time). then do this: >> >> >> Foo::initialize(); // global instance now available >> >> $instance = Foo::getInstance(); // wow, there really is an instance! >> >> echo $instance->instanceVariable; // holy shit, i just did what you >> >> said >> >> i >> >> couldn't >> >> > class Foo { >> >> > public static function initialize() { >> > return; >> > } >> >> > public static function getInstance() { >> > return $this; >> > } >> >> > } >> >> > Foo::initialize(); >> > $obj = Foo::getInstance(); >> >> > This code doesn't work because $this is undefined inside >> > getInstance(). It's undefined because there is no instance to refer >> > to inside that method. >> >> ok, bad example...i was recalling the commonly used getInstance where a >> public instance is created and set within the class as a variable...which >> is >> returned from the function - i just recalled it badly. however and >> none-the-less...self and parent, though not accessible external to the >> object are still class-wide, singleton instances of Foo...otherwise, >> other >> singletons within the same object would not be able to share or reference >> other interfaces of the class (variables, functions, etc.). meaning, the >> variable $instance would always be null when calling getInstance...when >> in >> reality, it will only ever be null one time. >> >> class foo >> { >> private $bar = 'hello world'; >> private static $instance = null; >> public static function getInstance() >> { >> if (is_null(self::$instance)){ self::$instance = new foo(); } >> return self::$instance; >> } >> public function pah(){ return $this->bar; }} >> >> $foo = foo::getInstance(); >> echo '<pre style="font:10px;">' . $foo->pah() . '</pre>'; >> >> my primary oversight leading to the bad example was being able to declare >> an >> entire class as static in other languages. as it is in php, you can't >> static >> function __construct() et. al. >> >> even so, Foo is always an instance...either self, parent, or $instance. >> the >> interface definitions determine whether or not (and how) that instance is >> consumed (internal and external)...not whether an instance itself exists. >> when i say 'instance' it is from a php perspective and not from the >> caller's >> instance-type notation, i.e. $instace->method() or static-type notation, >> i.e. foo::bar(). > > So, if I'm reading that right, you're saying that "self" and "parent" > always refer to the same instance of the class, regardless of what > instance they're used from. no, i'm saying that even static singletons have instances...where 'self' and 'parent' are how you reference the class or its parent. non-singletons wouldn't be of much use if they *didn't* refer to their respective, multiple instances...they'd of course be, well, singletons. ![]() > Consider the following code: > > class Foo { > > public $x; > > public function getX() { > return $this->x; > } > } > > class Bar extends Foo { > public function __construct() { > $this->x = 42; > } > > public function getX() { > return parent::getX(); > } > } > > class Baz extends Foo { > public function __construct() { > $this->x = 69; > } > > public function getX() { > return parent::getX(); > } > } > > $bar = new Bar(); > $baz = new Baz(); > > $y = $bar->getX(); > $z = $baz->getX(); > > If parent referred to the same instance no matter where it's used then > $bar->getX() and $baz->getX() should return the same thing. no, getX() is NOT static. you initially said that Foo::bar() would NOT create an instance of Foo. i'm saying it DOES and is referenced either as self or parent. i'd assume we'd still be working with *static* examples since that's what was being discussed. again, this demonstrates my point...look at getInstance, a static interface. were there no foo instance, even though static, then i couldn't ever bypass is_null(self::$instance)...it would *always* be null. class foo { private $bar = 'hello world'; private static $instance = null; public static function getInstance() { if (is_null(self::$instance)){ self::$instance = new foo(); } return self::$instance; } public function pah(){ return $this->bar; } } > Since > they don't we know that parent must be referring to the instance from > which it is used. hmmm, kind of sounds like there'd BE an *instance* of *extender* even though it's interface is static. ![]() |
|
|
|
#20 |
|
Messages: n/a
Hébergeur: |
Steve wrote:
> <snip> >> So, if I'm reading that right, you're saying that "self" and "parent" >> always refer to the same instance of the class, regardless of what >> instance they're used from. >> > > no, i'm saying that even static singletons have instances I thought a singleton _was_ an instance. In fact, a singleton is intended to be the _only_ instance of a class. Isn't that why they call them singletons? What, then, is a static singleton? > ...where 'self' and > 'parent' are how you reference the class or its parent. non-singletons > wouldn't be of much use if they *didn't* refer to their respective, multiple > instances...they'd of course be, well, singletons. ![]() > A non-singleton is just another instance, but instances don't have instances so I don't understand your comment, ".. non-singletons wouldn't be of much use if they *didn't* refer to their respective, multiple instances.." -- Visit <http://blogs.instreamco.com/anything.php> to read my rants on technology and the finance industry. Visit <http://tggagne.blogspot.com/> for politics, society and culture. |