
![]() |
Caligula
Ok stupid question.. but its driving me crazy... more and more scripts I pull apart I have been seeing something like this..
$this->info = array(); And for some reason... $this-> part is usually highlighted...which is what is throwing me off... I have tried looking it up.. but I cant find anything.... what is it doing? thedarkness
quote author=Caligula link=topic=157.msg905#msg905 date=1178263623 Ok stupid question.. but its driving me crazy... more and more scripts I pull apart I have been seeing something like this.. $this->info = array(); quote A pseudo-variable, $this is available when a method is called from within an object context. $this is a reference to the calling object (usually the object to which the method belongs, but can be another object, if the method is called statically from the context of a secondary object). As per http:// php.net/classSo, in the context of your post "info" is a variable (in this case an array) which is a member of a class (object) which $this points to (NOTE: it is an internal pointer, it cannot be used from outside the class scope). Here's an example; <? phpclass foo { private $info; function set_bar() { $this->info = "FUBAR"; } function print_bar() { print $this->info; } } // Usage $my_foobar =& new foo(); $my_foobar->set_bar(); // Will print "FUBAR" $my_foobar->print_bar(); ?> [edit]Removed moronicness in code.[/edit] HTH, td nutballs
your not an idiot, i had the same exact question about a week ago for perk.
Another way to explain it is that it is DOT notation. $this->info = array(); is the same as: $this.info = array(); <>info> is the method or property <>this> is the object though i am not sure if you are setting array or setting $this? perkiset
quote author=nutballs link=topic=157.msg919#msg919 date=1178293103 though i am not sure if you are setting array or setting $this? In this case NBs, you are setting a null array pointer to the $this->info member property/reference/variable/whatever you want to call it. In PHPthe garbage collection and variable construction is automatic. So the reason you do this is if you will need to call a function that REQUIRES an array to be created first (like implode()) or if you are cleaning up memory from a previous array. For example, if you had 10K elements in $this->info and wanted to throw them away and free the memory, but still keep info as an array, then by saying $this->info = array() is an easy way to effect that.Another reason for this notation is that you can both construct and populate the array in one pass. Consider: $this->info = array(0=>'Hello', 1=>'World'); would create the array info in the current class ($this) and set element [ 0 ] to 'Hello' and [ 1 ] to 'World' . You can do associatives like this as well: $this->info = array('key1'=>'avalue', 'anotherkey'=>'yet Another Value!'); /p nutballs
oh i get it.
its a constructor. not just a Object.Method call. $this is a variable initially not a class correct? info doesnt exists until it is set = the array that you make. array() is just an empty array. info is more like a "sub variable" correct? im guessing you can do $this->somerandomthingy = array() as well right? if so, i get it. perkiset
quote author=nutballs link=topic=157.msg927#msg927 date=1178295746 its a constructor. not just a Object.Method call. $this is a variable initially not a class correct? info doesnt exists until it is set = the array that you make. array() is just an empty array. info is more like a "sub variable" correct? im guessing you can do $this->somerandomthingy = array() as well right? if so, i get it. @ constructor - precisely. Arrays are just a bit different than primitives and as such, you create them with that function. They'll be auto-created if you were to do something like this: $myArr[] = 'Hello'; $myArr[] = 'World' ... so that the index reference is auto-incremented. Using array() is just more formal and tells PHPwhat your intentions are very clearly.@ $this - right on again. the $this C++ and Delphi equivalent is self. As you know, you'll code using the word $this in the class definition, but the $this context does not exist until an object has been created against that class... and then the $this is a reference to itself. $this can never be used outside of a class definition - it is a reserved word. @ info: consider the following - class myClass { private $anInt; protected $anotherInt; public $yetAnotherInt; var $thisVarDefaultsToPublicInt; function __construct() { $this->anInt = 0; $this->anotherInt = 1; $this->yetAnotherInt = 2; $this->thisVarDefaultsToPublicInt=32767; } } ... and now in the normal flow of code... $myObj = new myClass(); print $myObj->thisVarDefaultsToPublicInt; // prints 32767 print $myObj->yetAnotherInt; // prints 2 print $myObj->anotherInt; print $myObj->anInt; // Both of these fail because of their visibility in the class info in the previous examples is just a property or member or whatever their calling a class variable these days. QQ: Are you a strong OO guy or is this new stuff? /p Caligula
Ahhh OOP & classes... I haven't gotten that far yet... I head that stuff wasnt for beginners to
phpand that it was a matter of personal preference... I personally prefer something easy..lol... I want to spend more time <>scheming> planning...than codingBut thanks for finally explaining what that is...I can sleep now ![]() nutballs
ah ok. i get it. duh.
info is just a property that was declared in some class. just happens the assignment is inside that class, so $this is the shorthand so you dont have to be reliant upon the class name changing. got it. its a standard oop model. thedarkness
quote author=perkiset link=topic=157.msg928#msg928 date=1178297786 @ $this - right on again. the $this C++ and Delphi equivalent is self. Being picky here perk but I'm pretty sure the C++ equivalent of "$this->" is "this->" not "self->" isn't it? Cheers, td perkiset
You may well be right TD - It's been since the mid 90s that I've even touched C++ - that was Borland 3.1 and it was great, but I am pretty certain that I've forgotten anything pertinent... at least I was clear enough so that you could debug me
![]() m0nkeymafia
quote author=thedarkness link=topic=157.msg946#msg946 date=1178326787 quote author=perkiset link=topic=157.msg928#msg928 date=1178297786 @ $this - right on again. the $this C++ and Delphi equivalent is self. Being picky here perk but I'm pretty sure the C++ equivalent of "$this->" is "this->" not "self->" isn't it? Cheers, td Yes this-> is correct Does PHPhave dot notation? if so it may carry the same benefits of dot notation in C++ in that its faster [one less lookup]However I doubt it ![]() |

Thread Categories

![]() |
![]() |
Best of The Cache Home |
![]() |
![]() |
Search The Cache |
- Ajax
- Apache & mod_rewrite
- BlackHat SEO & Web Stuff
- C/++/#, Pascal etc.
- Database Stuff
- General & Non-Technical Discussion
- General programming, learning to code
- Javascript Discussions & Code
- Linux Related
- Mac, iPhone & OS-X Stuff
- Miscellaneous
- MS Windows Related
- PERL & Python Related
- PHP: Questions & Discussion
- PHP: Techniques, Classes & Examples
- Regular Expressions
- Uncategorized Threads