Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 2 1 2 LastLast
Results 1 to 10 of 17
  1. #1
    Senior Member Tomaszewski's Avatar
    Join Date
    Oct 2006
    Location
    Exton, PA
    Posts
    194
    Member #
    14132
    Yet, another question from Yours Truly! :ichatcool:

    What is the difference between these and what do they mean?!

    PHP Code:
    $this->$name vs$this=>$name
    for example, i'm running the code below.

    PHP Code:
        foreach ($buttons as $name=>$url)
            {
                
    $this -> DisplayButtons($width$name$url, !this->IsURLCurrentPage($url));
            } 
    and there is a part where the foreach starts

    PHP Code:
     foreach ($buttons as $name=>$url
    So now we use ( => ) but moving further, we see

    PHP Code:
    $this -> DisplayButtons($width$name$url, !this->IsURLCurrentPage($url)); 

  2.  

  3. #2
    Senior Member Brak's Avatar
    Join Date
    Apr 2003
    Location
    San Francisco, CA
    Posts
    3,413
    Member #
    1217
    Liked
    2 times
    The -> operator is signifying you're accessing a property/method of an object. The => operator is kind of a key/value signification (think of it as a modified =).

    So, you can create an associative array by doing:

    Code:
    $assoc_array = Array(
      "tomato" => "red",
      "apple" => "green",
      "banana" => "yellow"
    );
    
    $assoc_array["tomato"]; // returns "red"
    You can use objects like so:

    Code:
    class Fruit{
      var $color;
      
      function setColor($value){
        $this->color = $value;
      }
    }
    
    $tomato = new Fruit;
    $tomato->setColor('red'); // calls function setColor on the $tomato object
    $tomato->color; // returns "red";
    Kyle Neath: Rockstar extraordinare
    The blog | The poetry site | The Spore site

  4. #3
    Senior Member Tomaszewski's Avatar
    Join Date
    Oct 2006
    Location
    Exton, PA
    Posts
    194
    Member #
    14132
    OH CRAP! => Assigns a value to a key in arrays. DOH! Thanks sincerely!

  5. #4
    Senior Member Tomaszewski's Avatar
    Join Date
    Oct 2006
    Location
    Exton, PA
    Posts
    194
    Member #
    14132
    Ok, so can someone explain to me the use of -> in OO PHP.

    I'm not sure how something like the following works:

    PHP Code:

    $this 
    -> $attribute 

    to be more specific, if I have a function:

    PHP Code:
    class classname
    {
    var 
    $attribute;
    function 
    __get($name)
    {
    return 
    $this->$name;
    }
    function 
    __set ($name$value)
    {
    $this->$name $value;

    What does $this->$name = $value; do? According to me, this says. The value of $value into $name. Right? If so, then why bother with $this->?? Why not just:

    PHP Code:
    $name $value 

  6. #5
    Senior Member
    Join Date
    May 2003
    Location
    UK
    Posts
    2,354
    Member #
    1326
    Because using $this is the OOP thing to do.

    Your variable $name should only exist in your Class, so that is why you use $this, so that PHP knows you mean the variable in your class, and not, say, a variable outside of your class or in a function etc.

  7. #6
    Senior Member Tomaszewski's Avatar
    Join Date
    Oct 2006
    Location
    Exton, PA
    Posts
    194
    Member #
    14132
    AHHH ok! So does

    PHP Code:
     $this-> 
    only exit inside classes?

  8. #7
    Senior Member Shani's Avatar
    Join Date
    Nov 2004
    Posts
    1,140
    Member #
    8171
    I'm confused by your question. In OO php, isn't everything *technically* in a class? Maybe my confusion is due to only ever using PHP in an MVC framework...

    Using $this->variable in a method allows the variable to be accessible to outside the method; otherwise $variable is local.
    Shani

    I have an eye for detail like you'd never believe.

  9. #8
    Senior Member Brak's Avatar
    Join Date
    Apr 2003
    Location
    San Francisco, CA
    Posts
    3,413
    Member #
    1217
    Liked
    2 times
    Quote Originally Posted by DCScene
    I'm confused by your question. In OO php, isn't everything *technically* in a class? Maybe my confusion is due to only ever using PHP in an MVC framework...

    Using $this->variable in a method allows the variable to be accessible to outside the method; otherwise $variable is local.
    DEFINITELY not. In OO languages this is true, but in PHP pretty much *nothing* is a class. The only time you ever get an object (instance of a class) is when you invoke [minicode]new Something[/minicode]. The question about "local" (assuming you mean the awesome soup of private/public/scoped) variables is a whole other situation, and I'd advise you read a few articles on it since PHP's implementation is screwed three ways from Sunday.

    [minicode]$this[/minicode] does only exist inside of a class, and it's in reference to the current instance of the class. It can only exist within a class definition.
    Kyle Neath: Rockstar extraordinare
    The blog | The poetry site | The Spore site

  10. #9
    Senior Member Tomaszewski's Avatar
    Join Date
    Oct 2006
    Location
    Exton, PA
    Posts
    194
    Member #
    14132
    yea, i did learn about the public/private... etc.... but it is fuzzy! I'll save that question for another time though!

    Thanks for your replys!

  11. #10
    Senior Member Tomaszewski's Avatar
    Join Date
    Oct 2006
    Location
    Exton, PA
    Posts
    194
    Member #
    14132
    QUESTION 1 of 3: not to beat a dead horse, but... can someone confirm my thinking...

    PHP Code:
    $query "select * from books where " .$searchtype" like '%" .$searchterm"%'";

    $result $db->query($query); 
    I'm still confused on the whole f@#$ing ( -> ) thing!!! I understand the top line puts into variable $query the result of the mysql search. But then...

    we are assigned $result, the result of $query. Yet, why are we putting it into a function query() ?!?!?!

    Basically the last line has got me all knotted up in the head!

    :::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::

    QUESTION 2 of 3: Can I say that:

    PHP Code:
    $num_results $result->num_rows
    is the same as writing

    PHP Code:
     $num_results num_rows($result); 
    :::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::

    QUESTION 3 of 3: www.php.net does not list a function num_rows(), but it does list a function mysql_num_rows(). The same goes for fetch_assoc() and mysql_fetch_assoc() ... is the author of the book i'm reading on crack? Or am I?


Page 1 of 2 1 2 LastLast

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 03:03 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2022 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com