1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

What is -> and => in php

Discussion in 'PHP' started by gilgalbiblewheel, Jul 8, 2018.

  1. #1
    I've been using these without understanding them.
     
    gilgalbiblewheel, Jul 8, 2018 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #3
    The best way to think of them is 'propery of' and 'value in' respectively.

    'property of' relates to objects, saying that "the property of this object" much akin to what a period does in C++ and JavaScript. An object property can be a variable or a function. When it's a function they are referred to as 'methods'.

    
    class TestClass {
    	public var1 = 3;
    		
    	public function start() {
    	}
    }
    
    var test = new TestClass(); // create new test object
    
    echo test->var1; // accesses the variable
    
    test->start(); // run the function
    
    Code (markup):
    There is another way of accessing properties/methods on objects where two colons "::" are used. This is for accessing the base class's properties such as statics and 'self' (a complex/advnanced topic -- read @nico_swd's links to php.net).

    'value in' is an array thing, where PHP doesn't have "real" arrays in a programmatical sense and instead allows you to create "associative arrays", where each entry in the array instead of (or in addition to) having a numbered index can also be named/labelled. This name is called a "key" with the entire structure called a "key/value pair".

    
    $test = [
    	'one' => 1,
    	'two' => 'John'
    ];
    
    echo $test['two']; // outputs 'John'
    
    Code (markup):
    That's really all there is to it.
     
    deathshadow, Jul 13, 2018 IP
    sarahk likes this.
  4. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #4
    I understand the question is genuine and innocent but it would be less effort to simply google it.
     
    NetStar, Jul 13, 2018 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #5
    There's always the possibility they did -- or even read the manual -- and didn't understand it. I get that a lot when I'm learning things as the way I think about solving problems... differs from how other people think about things.
     
    deathshadow, Jul 13, 2018 IP
  6. Phil S

    Phil S Member

    Messages:
    60
    Likes Received:
    18
    Best Answers:
    4
    Trophy Points:
    35
    #6
    I remember my beginnings as a programmer, having innately grasped procedural concepts early on, I had no trouble writing complicated programs. But then came the OOP : Classes, objects, instances, methods, attributes would always make me ponder why on earth would they want to ditch procedural for OOP, computers are sequential machines, there is no reason to even do that, I thought.

    Think of your OOP program as a biome, where communities of different creatures co-exist and do their business.
    We all know dogs bark, wag their tails, some are small, some stink and have fleas... you get the idea.
    $new_doggo = new Dog("male","pug");
    $new_doggo->bark();

    Basically :
    * attribute : describes what your object CAN BE, if you make it that (keywords : construct, new)
    * method : describes what your object CAN DO, if you asked him to.

    All you have to do now is design your objects and make them do stuff you want done, it's that simple, not any less intuitive compared to a procedural approach.
     
    Phil S, Jul 14, 2018 IP
    sarahk likes this.
  7. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #7
    I think OOP came easier for me because I learned about records/structures FIRST. A Wirth language "RECORD" or C syntax "STRUCT" is just the precursor to objects -- a set of variables associated with one data record. Objects just add methods -- functions -- to that object that have a innate affinity for the data in that record.

    I mean literally, a "class" is just a "struct" with functions on it.

    It also helped I learned memory management -- MANUAL memory management -- first, knowing the difference between the stack and the heap, so constructors and destructors just made sense -- a startup routine automatically called when the memory for the object is allocated onto the heap, and a routine to automatically call for cleanup when the object is de-allocated from memory.

    It's not that complex, but I SWEAR some languages seem to intentionally make the process as obtuse as possible. (see C++). I know if I had to LEARN objects from how C++, Java, or PHP implement them, I'd have a snowballs chance in hell of learning a blasted thing. It's only my foundation in Object Pascal, Smalltalk, Modula, and Ada that allows me to be as competent as I am in PHP or client side with the DOM in JavaScript.

    More so with DOM manipulation since I learned how to build binary tree for sorting decades before JavaScript was a twinkle in Brendan Eich's eye.
     
    deathshadow, Jul 14, 2018 IP
  8. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #8
    In simple layman terms,
    -> is used to "extract" value from an object.
    => is used to "assign" value to an array.

    Object is like a house. You enter main door first, then the door of a room, and then the door of the bathroom, and this is when you can access the toothbrush.

    $brush = $house->$room->$bathroom->ToothBrush;

    So now $brush has the value stored in "ToothBrush"

    => used to store new value into array
    $array=array(
    'color1' => 'red',
    'color2' => 'white',
    'color3' => 'black'
    );

    And you can extract these values as $c1 = $array["color1"];
     
    JEET, Jul 25, 2018 IP