Posts by Wallace Maxters • 102,340 points
1,984 posts
-
3
votes3
answers176
viewsQ: Is there a difference between "str(my_object)" and "my_object. __str__()" in Python?
I observed that, in Python, when we have an object with the method __str__, it is responsible for returning a string representing the object - or something like that. Example: from uuid import uuid4…
pythonasked Wallace Maxters 102,340 -
3
votes1
answer62
viewsQ: Is it a good idea to use interfaces to specify which magic methods a class will implement?
I’ve seen some code in PHP that, to check if a class has the method __toString, the function is used method_exists. Example: if (method_exists($object, '__toString')) echo $object; On the other…
-
9
votes1
answer939
viewsQ: What is the name of the :: (double-point) operator in PHP?
In PHP, it seems that this operator :: has a Hebrew name (I don’t know why anyone would use Hebrew), Paamayim Nekudotayim. I always have a problem when I have to explain to someone the name of that…
phpasked Wallace Maxters 102,340 -
7
votes1
answer1032
viewsQ: Basic difference between Abstract Factory and Factory?
I was taking a look at Manual for PHP and saw an example of implementing the standard Factory. Example: class Exemplo { // Método Factory parametrizado public static function factory($type) { if…
pattern-designasked Wallace Maxters 102,340 -
5
votes3
answers949
viewsQ: Why does the User-Agent header always return "Mozilla /5.0" independent of the browser?
I know that in PHP we can access a header called User-Agent and thus discover information about the operating system and browser used by the client. The only thing I don’t understand is that…
-
0
votes3
answers233
viewsA: Convert the date format the user types
I believe that the best way to revolve situations of the type, is with the object Datetime. It is always good to try to use the new features of PHP. $data = '17/07/2014'; $data_en =…
-
1
votes1
answer118
viewsQ: Find the index of an array that matches a regular expression
According to the PHP Handbook, the function preg_grep returns the entries of a array which combine with regular expression. Example $array = [ 'Banana', 'Maçã', 'Café', 'Biscoito', ]; $array_com_B =…
-
1
votes1
answer137
viewsQ: How could you create the array_column function in versions prior to PHP 5.5
According to the PHP Handbook, the function array_column is available from PHP 5.5 It returns the value of a "column" of a multidimensional array in a single array. Example: $records = array( array(…
-
1
votes2
answers241
viewsA: Convert string into array along with delimiter
Wouldn’t that be something? $regex = 'Filme com pipoca'; if (preg_match('/com|by|para|por/u', $string)) { $array = preg_split('/\s+/u', $string, -1, PREG_SPLIT_NO_EMPTY); } Upshot: ['Film', 'com',…
-
2
votes4
answers2514
viewsQ: Is there a difference between the is_file and file_exists function?
Actually, there is some difference between the function is_file and file_exists? var_dump(file_exists('public/index.php'); // bool(true) var_dump(is_file('public/index.php')); // bool(true);…
phpasked Wallace Maxters 102,340 -
3
votes2
answers435
viewsA: What are Exceptions and how should I create and embed them in PHP
I can help you with the question: How could I standardize my exceptions and log logs of automatically released exceptions? If I understand correctly, you would like to release a log when the…
-
2
votes2
answers878
viewsA: How to create a unique method to handle errors with ajax
The way @Brunno did, you would be setting an error that would be standard for all XHR requests on your page. Another way to do it is to declare a function that returns the error as a means of…
-
15
votes2
answers22340
viewsQ: What is assert in Python for?
The @Ciganomorrisonmendez gave me a reply in a previous question on Python. And as I’m a beginner in Python still, I didn’t know what the assert that he indicated in the reply. The excerpt from the…
pythonasked Wallace Maxters 102,340 -
3
votes2
answers589
viewsQ: Read the last 5 lines of a file through Python
I asked that question here at Stackoverlow Display last 5 lines of a PHP file? Now I wonder how I could do the same thing in Python. How could I get the last 5 lines of a large file without having…
-
4
votes1
answer220
viewsQ: Why does Python allow you to overwrite the value of False and True?
I’m studying Python and in these studies I came across something curious. > False = True #Imprime: True > print(False) #Imprime: True > False = bool(0) > print(False) #Imprime: False I…
pythonasked Wallace Maxters 102,340 -
1
votes1
answer410
viewsA: Change directory to HTML to understand src target
Already tried to use tag base? With it it is possible to set the base url for all page links. Example: <head> <base href="http://localhost/pasta/"> </head>…
-
37
votes2
answers2324
viewsQ: Why do they say using arroba to suppress errors is bad practice?
In PHP, we can suppress errors using arroba before certain expressions. This can be seen in: What is the function of@at the beginning of PHP expressions I already watched some Hangouts on PHP and…
-
4
votes4
answers1277
viewsA: View last 5 lines of a PHP file?
Based on @Daltonmenezes' wonderful solution, I developed a class with the method tail, to perform such an operation. Let’s see: class FileObject extends SplFileObject { public function tail($amount)…
-
5
votes1
answer627
viewsQ: What is the difference between reset and clear in Terminal?
In the Terminal, what is the difference between the commands reset and clear? I’m using the Terminal of Ubuntu and apparently the two commands clear the screen.…
-
5
votes4
answers1277
viewsQ: View last 5 lines of a PHP file?
Through the Shell, I can display the last 5 lines of a file through the command tail. Thus: > tail -n 5 public/index.php I would like to do the same thing in PHP. But I didn’t want to load the…
-
-2
votes3
answers218
viewsQ: What makes the expression "'2 pigs' + '3 horses' == '5 animals'" return true in PHP?
I’ve asked a similar question here at Stackoverlow at: Why in PHP the expression "2 + '6 apples'" is 8? However, taking a look at some strange things curiosities that exist in PHP, I saw that the…
-
0
votes1
answer133
viewsQ: How can I read Docblock from a PHP method to a string?
I wonder how I can read the Docblock of a PHP method and turn it into a string. For example, I have the following code: class Stack { /** * @return void */ public function overflow() { } } How could…
-
2
votes1
answer159
viewsQ: How to define routes in Laravel that only work in a development environment?
I would like to know how to define routes in Laravel that work only in development environment. I believe that this can be useful to facilitate the debugs. I want to be able to separate some routes…
-
2
votes1
answer159
viewsA: How to define routes in Laravel that only work in a development environment?
In Laravel, it is possible to detect whether the environment in which the system is running on production or local. The app/start/local.php file In the case of local development, we have the file…
-
1
votes2
answers60
viewsA: How to "simulate" a Generator in versions prior to PHP 5.5?
To simulate a Generator in versions prior to PHP 5.5, it would be necessary to create a class that implements Iterator. See simply how it would work: class XRange implements Iterator { protected…
-
11
votes3
answers1792
viewsQ: Is it a good idea to declare variables with accents?
I was taking a look at the PHP Handbook on the variables. There I found an excerpt of code that left me with the "foot behind". $täyte = 'mansikka'; // válido; 'ä' é um caracter ASCII (extendido)…
-
3
votes2
answers60
viewsQ: How to "simulate" a Generator in versions prior to PHP 5.5?
From PHP 5.5 we can use yield instead of return functions and, with this, we create Generators. The reasons for using the yield can be seen here What are the advantages of using a Generator (Yield)…
-
1
votes2
answers1654
viewsA: Php and guzzle, how do you get to an address and pick up what you return?
Correct answer from @Rodrigorigotti. There is also another way to convert the data, which is by using the magic method __toString. It can be done like this: $body = (string) $response->getBody();…
-
3
votes2
answers13570
viewsQ: How to make a scroll to the end of a div?
I’m hoping a determined div, possessing the attribute overflow-y:auto, whose height is fixed, is rolled at the end, so an element is added to it. In this case, I need it to roll until the end,…
-
8
votes5
answers51834
viewsA: In Javascript, how to verify that an object is empty (without jQuery)?
One way to make this operation quick is to also use the `toSource method`. Behold: var vazio = {} var naoVazio = {ola: 'mundo'} function isEmptyObject(obj) { return obj.toSource() === "({})"; }…
-
16
votes5
answers51834
viewsQ: In Javascript, how to verify that an object is empty (without jQuery)?
For jQuery, I can tell if a Object is empty as follows: $.isEmptyObject({}); // true $.isEmptyObject(window); // false To find out if a array is empty, we can do the same, but without jQuery would…
-
0
votes2
answers6534
viewsA: How do PHP read the command line entry as a string?
This can be done through the function fgets combined with the constant STDIN. Behold: $line = fgets(STDIN); PHP script echo "iniciando a aplicação php\n"; $line = fgets(STDIN); echo "O resultado é…
-
1
votes2
answers6534
viewsQ: How do PHP read the command line entry as a string?
In the Python, I know that to read the data entered by the command line we use the function raw_input. Example: Python script: print 'digite algo para inicializar' resultado = raw_input() print 'O…
-
4
votes2
answers198
viewsA: How can I do a validation to check if a variable is empty and is a string in php
To leave one more option, the simplest way to do it is like this: if ($string === '') { } With Trim, to make sure nothing goes empty: if (trim($string) === '') { } The operator === checks if the…
-
6
votes3
answers2715
viewsQ: Is there any way to display the size of the string in a MYSQL result?
Is there any way to display the string size in a MYSQL result? I need, for example, to know the size of a given string returned in a SELECT with group_concat. What I want would be more or less like…
mysqlasked Wallace Maxters 102,340 -
5
votes2
answers230
viewsA: How to convert a yaml to PHP array?
A good way would be using the Yaml Component of Symphony. Behold: use Symfony\Component\Yaml\Parser; $yaml = new Parser(); $value = $yaml->parse(file_get_contents('/arquivo.yml')); Reference: The…
-
3
votes2
answers230
viewsQ: How to convert a yaml to PHP array?
How could I do to convert data yaml in a array of PHP? Yaml example: Usuario: nome: Wallace idade: 25 linguagens: - PHP - Python - Javascript…
-
3
votes2
answers374
viewsQ: What are the valid delimiters for preg_regular expressions?
What are the valid delimiters for regular expressions in function preg_*? I know we can use some of these (/, ~ and #), as shown in the example below: $numero = '0.12.13'; preg_replace('/\D+/', '',…
-
3
votes2
answers97
viewsQ: What does the expression "for x in *" do?
I saw this expression in a SOEN question, but I did not understand very well what it does for x in *; do echo $x; done This printed the folder list of the directory I was in. That’s what that…
bashasked Wallace Maxters 102,340 -
13
votes3
answers666
viewsQ: In bash, what is the difference between <, << and <<?
I’m learning now to mess with bash (Ubuntu terminal) and would like to understand the difference between these three operators. I think that < It’s file-related, but I can’t explain what he does…
bashasked Wallace Maxters 102,340 -
7
votes1
answer11081
viewsA: Remove duplicate elements in Array
The PHP Handbook does not detail this in the English version of the function array_unique, but in the English version, it shows that this function has a second parameter. See the "skeleton" of this…
-
5
votes3
answers2129
viewsQ: What is the simplest way to create a mask for numbers in PHP?
I have two ways to create masks for numbers in PHP, but I don’t know if it’s the most elegant and effective, so I would like to have some options to improve the code. I need the function to behave…
-
4
votes3
answers1611
viewsA: Dynamic property name in Javascript
As a complement to the previous answers, it is also possible to dynamically verify whether the property has been defined, as follows var nomePropriedade = "Propriedade1"; var meuObjeto = { "nome" :…
-
2
votes1
answer993
viewsA: Laravel Session - Public for Controller
First, you should check the configuration of the Laravel. Laravel (like most frameworks), does not use the native PHP session. I can’t remember if there’s any way to set this up. Thus, the sessions…
-
3
votes3
answers419
viewsA: Assign type to parameters
With quoted by @Maniero, it is not possible to assign type induction to a function or method when it comes to scalar values. I believe the only way in PHP to force type parameters scalar in PHP…
-
1
votes3
answers566
viewsA: Call construction class
You are not calling the class in the constructor. You are inducing the type that will be accepted by parameter. See what your statement is for: class X { private $classe; public function…
-
3
votes1
answer7210
viewsQ: What is the difference between & and && in SHELL?
I’m using the terminal of Ubuntu. I learned a certain command to free the memory, which is : free & sync In another case, I learned something like nohup && After all, what are these two…
shellasked Wallace Maxters 102,340 -
1
votes5
answers860
viewsA: Why is a variable with no value considered uninitiated?
In PHP, variables are evaluated as false for isset in two cases: When it doesn’t exist When she’s null. For the two cases below, the evaluation of isset will return false. var_dump(isset($a)); $b =…
-
2
votes1
answer3472
viewsQ: String comparison (which answers hours) in javascript is reliable? How does it work?
It is known that, in terms of time, that 07:30 is less than 08:30 - speaking of hours. In javascript, if we make this comparison, everything goes as expected above '07:30' < '08:00' // true…
-
2
votes5
answers444
viewsA: PHP array types and values
Array vs Objects That "little arrow" there $pessoas->nome is the Object Separator. He is responsible for access to members belonging to an object (whether property or method). In the first case…