What is the difference between collection and array in PHP?

Discussion in 'PHP' started by gandalf117, Jul 11, 2013.

  1. #1
    Is there a difference between the two at all?
    If there is could you give an example for each?
     
    gandalf117, Jul 11, 2013 IP
  2. EmmanuelFlossie

    EmmanuelFlossie Active Member

    Messages:
    159
    Likes Received:
    11
    Best Answers:
    2
    Trophy Points:
    65
    #2
    Makes no sense to me what you just asked, could you clarify please?
    A array is a collection!

    
    <?php
    $myArrayCollection = array('a', 'b', 'c');
    $myArrayCollection = array('a' => 'Hello', 'b' => 'World', 'c' => '!');
    ?>
    
    Code (markup):
     
    EmmanuelFlossie, Jul 11, 2013 IP
  3. ColorWP.com

    ColorWP.com Notable Member

    Messages:
    3,121
    Likes Received:
    100
    Best Answers:
    1
    Trophy Points:
    270
    #3
    There are no collections in PHP. They are Java langauge structure.

    PHP uses variables (array, int, string, bool) and objects (classes).
     
    ColorWP.com, Jul 11, 2013 IP
    sarahk and ryan_uk like this.
  4. Tony Brar

    Tony Brar Active Member

    Messages:
    220
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    75
    #4
    I've never heard of a collection in PHP, you're probably thinking of an array.
    An array is a type of variable in PHP.
     
    Tony Brar, Jul 11, 2013 IP
  5. gandalf117

    gandalf117 Active Member

    Messages:
    111
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #5
    I hadn't heard of a collection in PHP neither until I started reading the cookbook of Symfony 2 which is based on PHP. Here are a few lines from it:


    Now what do they mean by defaults collection? How is defaults a collection here? Am I supposed to know this stuff from PHP?

    Here is some more:


     
    gandalf117, Jul 13, 2013 IP
  6. scriptjerk

    scriptjerk Active Member

    Messages:
    43
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    58
    #6
    Sounds to me like there is not real difference. The ArrayCollection object is an array, and when used inside whatever class it gives you more operational functionality like an object. In php, i can define an object out of a class , but it can be an array form, its just being from a class, its kinda officially an object.
     
    scriptjerk, Jul 13, 2013 IP
  7. ColorWP.com

    ColorWP.com Notable Member

    Messages:
    3,121
    Likes Received:
    100
    Best Answers:
    1
    Trophy Points:
    270
    #7
    As I said above:


    Basically, the $products property (which is just a regular variable, but found inside a class, which is why we call it a property) is an object (an instance of the ArrayCollection class in this case). I am guessing it is a helper class included in Symfony2 by default. However, it has nothing to do with the core PHP and its language constructs, variable types or syntax. It is just a custom class provided by Symfony which you can use to create objects (instances of that class).

    I am guessing you took your quote from here? If so, I don't know if you have noticed, but each code block on that page has tabs above it for different language syntaxes. The one you copied is for YAML, not PHP. Here's the one for PHP, which should make much more senses:
    use Symfony\Component\Routing\RouteCollection;
    use Symfony\Component\Routing\Route;
     
    $collection = new RouteCollection();
    $collection->add('blog', new Route('/blog/{page}', array(
        '_controller' => 'AcmeBlogBundle:Blog:index',
        'page'        => 1,
    )));
     
    return $collection;
    PHP:
    Wherever you see the word "new" before a funny looking word, it means we are creating a new object/instance of the class that has the funny looking word. You should look more into the OOP aspect of PHP to gain deeper understanding of the concept.

    Also, if you think I've helped you a bit, don't forget to select my answer as the solution to this thread.
     
    ColorWP.com, Jul 14, 2013 IP