Custom typing

1
  

According to DOC Type Manipulation , the allowed conversions are:

(int), (integer)          - molde para inteiro
(bool), (boolean)         - converte para booleano
(float), (double), (real) - converte para número de ponto flutuante
(string)                  - converte para string
(array)                   - converte para array
(object)                  - converte para objeto
(unset)                   - converte para NULL (PHP 5)

I was wondering if there is any way to create a custom typing. I looked in the documentation and the SO-EN but found nothing about it.

I already have a function for this purpose, but I wanted to create a typing type (upper) 'Papa Chalie' // PAPA CHARLIE , would it be possible?

    
asked by anonymous 14.11.2016 / 16:17

2 answers

3

PHP does not really have cast . It has functions that do value conversion. Remember that PHP is dynamically typed, so the variable type does not matter and cast serves to match a value with a variable.

To convert values any function serves. You will not have the cast syntax, but it's something good to make clear that this is a conversion. Then create a function that interprets the values of the received object and create a new object within the possibility of it. This function can be loose or can be inside a class, can be static or instance.

In fact the example given if it could be used would be a complete abuse of the syntax since it does not even change the type.

See how it works in C # to get an idea how this operator in the background is a function which allows an appropriate syntax when it is a coercion of type . PHP does not have this.

    
14.11.2016 / 16:40
3

Not possible.

Note also that cast or data conversion is used to transform one data type to another. What you are trying to do is to transform the 'format' or value from a variable (string) to the upper box, and the path is to use a function.

No need to implement a function to do this, there are php native functions strtoupper , mb_strtoupper .

In response because 'a' === 'A' is false: I'll try to respond in a very informal, non-technical way. It is because they are different values, notice that the === operator checks by type and value, and 'a' is different from 'A' as much as 'b' . Internally, each letter has a value number, note the value of 'a' has to be different from 'b', 'c', and also 'A', so we can for example print, compare, on the other.

    
15.11.2016 / 05:21