0

I'am .NET C # programmer and is now learning PHP programmering.

I found PHP a little confusing. I hope somebody here can help me to clarify the following questions:

  1. Do PHP have "dynamic data collection" containers?.
    • I want to gather OBJECTS in one data-container ?

I have read php toturial and not found it!

In C #, we have the following, dynamic data-colection containers:

  • List, ArrayList, Dictionary, HashSet
Imran Khan
  • 39
  • 1
  • 4

1 Answers1

0

PHP is not a strongly typed language to begin with. However, it has an OOP capabilities. You can always use $mixed type in any of your containers, or even declarations.

With that said, Your Containers for Objects will work without trying to rely on C# type List Generic or any array collections of object.

But you can specify the type whenever you want to.

This is only applicable for Php 4, 5 and 7. Somehow, in PHP 6 they undermined the development of OOP and remove some of it. reining PHP 5 as the stable PHP.

Math function has been added in the later versions.

array list ( mixed $var1 [, mixed $... ] )

As you can see in here. You can always make 'mixed' as the type of your Class or Data Type, just like you use var for C#.

However, If you strictly want to use CLASS Generics, you would also need to require the Class before you can use it as a data type.

array list can be foreach looped or any kind of collection method calls.

But using generic array (['test' => 'test']); Does not make any big difference.

With the approach that you want, it is just like you, want to implement pragma strict. Being well accustomed C# strongly typed. I don't blame you for trying to find the same approach. It just that, no matter how clean you want your code to be. PHP will always allow different type to be added in the list if not specified.

Cause of PHP is a scripting language, not a compiled Language.

Aizen
  • 1,807
  • 2
  • 14
  • 28
  • Well.If you are working in php MVC and you want to send a data-collection to View. – Imran Khan Oct 11 '16 at 18:02
  • Then you have to make collection list... how to do that? – Imran Khan Oct 11 '16 at 18:02
  • Just like in C#. It is common to make your Business Logic first. Since you mentioned MVC. You have to do it in your Model Data. You can always Create a new View Model Class. I hope you know what a `ViewModel` is. Then specifically create a Property with get set then Just Pass that Model to the View like you normally would. In your Business Model all you have to do is Add the collection to the VM and pass it into the View. No Special work around, if you know how to do it in C# MVC 3 or later, there is no difference in PHP. Unless you're not familiar in PHP OOP. – Aizen Oct 12 '16 at 03:21