SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $this-> / What is it called? What does it do?

    Please consider
    function COutput($page, $entrypos, $phrase) {
    $this->page = (int)$page;
    $this->entrypos = (int)$entrypos;
    $this->phrase = $phrase;
    ...

    The function COutput is passed three parameters. Those three parameters are known as ($page, $entrypos, $phrase) both within the entity which calls the function and now, within the function. Changing them within the function does not affect the others. Is that right?

    Question: Does
    $this->page = (int)$page;
    $this->entrypos = (int)$entrypos;
    $this->phrase = $phrase;

    within the function create 3 further instances of those variables ($page, $entrypos, $phrase) known throughout the class to which COutput belongs, or is defined, or what else? What purpose do they serve?

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Variables inside the functions of a class such as $name are local - they cannot effect the variables outside the class.

    When making a class, you define variables mean for global use - and to access these in functions, you use the '$this->'.

    Does this help you understand it, or should I give a few examples?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bgsomers View Post
    Please consider
    function COutput($page, $entrypos, $phrase) {
    $this->page = (int)$page;
    $this->entrypos = (int)$entrypos;
    $this->phrase = $phrase;
    ...
    Presumably, something is missing from that example? You can't refer to $this, unless the function is member of a class.

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $this->

    Aha. Thanks. Then
    $this->page = (int)$page;

    within the function, sets a class-wide variable called $page equal to the variable $page within the function. Is that right?

    Does $this-> have a reasonable name?

  5. #5
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bgsomers View Post
    Aha. Thanks. Then
    $this->page = (int)$page;

    within the function, sets a class-wide variable called $page equal to the variable $page within the function. Is that right?
    Not really. It sets a member-variable.The scope of $this is the object instance. If you have multiple instances of your class, then they wouldn't share the member variables.

  6. #6
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes. Function COutput is defined within a class COutput.

    class COutput {

    var $datacount;
    var $page;
    var $entrypos;
    var $phrase;
    var $indexsize;

    ,,,

    function COutput($page, $entrypos, $phrase) {
    global $lang;

    $this->page = (int)$page;
    $this->entrypos = (int)$entrypos;
    $this->phrase = $phrase;

    Does $this->page within the function refer to the variable defined above with
    var $page; ?

  7. #7
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yep
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  8. #8
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bgsomers View Post
    Does $this-> have a reasonable name?
    $this is a special variable. It points to the current object instance. -> is a deference operator. In most other languages, a dot (.) is used instead. You can defer members of any variable, holding an object. For example:
    PHP Code:
    class Gadget {
      public 
    $name;
      function 
    setName($name) {
        
    $this->name $name;
      }
    }
    $my_gadget = new Gadget();
    $my_gadget->name "A nice gadget";
    var_dump($my_gadget);
    $my_gadget->setName("An uber-nice gadget");
    var_dump($my_gadget); 
    Output:
    Code:
    object(Gadget)#1 (1) {
      ["name"]=>
      string(13) "A nice gadget"
    }
    object(Gadget)#1 (1) {
      ["name"]=>
      string(19) "An uber-nice gadget"
    }

  9. #9
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's actually dereference operator http://en.wikipedia.org/wiki/Dereference_operator

    To access a member variable from within a member function, you must use the special variable (also called a pseudo-variable) $this. It refers to the current instance of the object itself. You combine it with the dereferencing operator (->) to access the member variables.
    my mobile portal
    ghiris.ro


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •