PHP is not a language with much type discipline.
In PHP there is a resource called array. The array can be a numbered or indexed list - and can also be both at the same time.
For example:
$arr = [];
$arr[] = "Stack";
$arr[] = "Over";
$arr[] = "Flow";
Upshot:
[
"Stack",
"Over",
"Flow",
]
In that case, the order of array
does not depend on the numbering/naming an index receives, but on the order it displays.
Then this below would work:
$arr[98] = "Stack";
$arr['96'] = "Over";
$arr[97] = "Flow";
[
98 => "Stack",
96 => "Over",
97 => "Flow",
]
Splfixedarray - an SPL resource
In my opinion, the PHP engine closest to presenting in the question is the class SplFixedArray
. This class is part of the standard PHP library, called SPL. With this class, it is possible to determine an object with a list, with a limited number of elements.
Behold:
$arr = new SplFixedArray(3);
$arr[] = 1;
$arr[] = 2;
$arr[] = 3;
$arr[] = 4; // Lança uma exceção, pois o número é limitado.
But back again to the beginning of that answer, the resource you need would be the array same. The SplFixedArray
guarantees the limit of elements that will be added, but has no type restriction.
You can make an implementation
In PHP there are two special interfaces called ArrayAccess
and Iterator
. With these two, you can create a class by determining the type of elements that can be added to your list.
Here’s an example I put together for you:
class StringList implements ArrayAccess, IteratorAggregate
{
protected $items = [];
public function offsetSet($key, $value)
{
if (is_string($value)) {
$key ? $this->items[$key] = $value : array_push($this->items, $value);
return $this;
}
throw new \UnexpectedValueException('Essa é uma lista que aceita somente string');
}
public function offsetGet($key)
{
return $this->items[$key];
}
public function offsetExists($key)
{
return isset($this->items[$key]);
}
public function offsetUnset($key)
{
unset($this->items[$key]);
}
public function getIterator()
{
return new ArrayIterator($this->items);
}
}
The use of the class would thus be:
$list = new StringList;
$list[] = 'Wallace';
$list[] = 'Maniero';
$list[] = 'Denner Carvalho';
$list[] = 1; // Se fizer isso vai lancar exceção
foreach ($list as $string) {
var_dump($string);
}
See it working on Ideone.
Note: The class created above is just one example. It is up to each one how to implement some resource, but I do not think it is feasible to do as exemplified, since PHP is a language weakly tipada (Don’t go out with a master of OOP ;) ).
You still want to go further with this?
In PHP 7, there is a feature that allows you to define the type of a function’s argument. Combining this with the operator ellipsis
, you can make a "hack" to create a array (list style) with specific types.
Behold:
function int_list(int ...$strings)
{
return $strings;
}
int_list(1, 2, 3, 4, 5);
If you added one string
(not numerical), would return an error:
int_list(1, 2, 3, 4, 5, 'x');
Error returned:
Argument 6 passed to int_list() must be of the type integer, string Given
Other resources available in the SPL
In addition to being able to implement, PHP also offers some new features, such as the class called SplObjectStorage
, which has the purpose of working with object storage.
In addition to the above, we also have:
- Spldoublylinkedlist
- Splstack
- Splqueue
- Splheap
- Splmaxheap
- Splminheap
- Splpriorityqueue
- Splfixedarray
- Splobjectstorage
Besides this little list there, maybe it is important to quote about the Generators.
Of course, these resources will never compare to a language tipada, as in the case of Java or C#.
Read more:
Well in PHP you can manipulate a list, more or less like this there, but you won’t be able to restrict the types of data that the list will receive.
– Euler01
Related:Working with lists without using Array() in PHP
– user28595
I agree with almost everything you are saying, but I can create an enumeration without
if
with type passed in the methods (already it happened this in previous version of PHP and now strongly in version 7) with return also of a certain type. Of course it’s notGenerics
equal C# or Java, but, I can create the particular enumeration, which in my view depending on the case does not need, imagine creating an enumeration for eachclasse
would be bad for development.– novic