{"id":18327,"date":"2024-09-11T08:00:00","date_gmt":"2024-09-11T11:00:00","guid":{"rendered":"https:\/\/king.host\/blog\/?p=18327"},"modified":"2025-06-02T16:47:49","modified_gmt":"2025-06-02T19:47:49","slug":"debug-com-var-dump","status":"publish","type":"post","link":"https:\/\/king.host\/blog\/tutoriais\/debug-com-var-dump\/","title":{"rendered":"Guia Pr\u00e1tico e Confi\u00e1vel Sobre Debugging com var_dump() e Xdebug no PHP"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">Depurar c\u00f3digo \u00e9 uma parte essencial do desenvolvimento de software, especialmente em linguagens din\u00e2micas como PHP. Entre as ferramentas mais comuns para esse prop\u00f3sito est\u00e3o o var_dump() e o Xdebug, que ajudam pessoas desenvolvedoras a entender o comportamento do c\u00f3digo e a identificar onde os problemas est\u00e3o ocorrendo.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">No PHP, o var_dump() \u00e9 amplamente utilizado por sua simplicidade e efic\u00e1cia, enquanto o Xdebug oferece recursos avan\u00e7ados para uma an\u00e1lise mais profunda. Quer saber mais sobre estes recursos? Continue a leitura deste artigo e confira:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">o que \u00e9 debug e por que ele \u00e9 importante;<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">como usar o var_dump() no PHP;<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">como utilizar o Xdebug para depura\u00e7\u00e3o avan\u00e7ada no PHP.<\/span><\/li>\n<\/ul>\n<h2><b>O que \u00e9 debug e por que ele \u00e9 importante?<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">No contexto do PHP, onde o c\u00f3digo \u00e9 interpretado em tempo real, podem surgir erros que n\u00e3o s\u00e3o detectados at\u00e9 o momento da execu\u00e7\u00e3o. \u00c9 aqui que o debug se torna essencial, permitindo que os desenvolvedores inspecionem o c\u00f3digo, entendam o fluxo de execu\u00e7\u00e3o e corrijam quaisquer problemas encontrados.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Sendo assim, ferramentas de debug ajudam a monitorar partes espec\u00edficas do c\u00f3digo, permitindo que a pessoa desenvolvedora identifique a causa raiz de um problema. Para isso, \u00e9 preciso ativar o display_errors no php.ini e deixar como padr\u00e3o \u201cmostrar todos os erros\u201d. Caso contr\u00e1rio, a tela aparecer\u00e1 em branco em exce\u00e7\u00f5es geradas pela aplica\u00e7\u00e3o e n\u00e3o ser\u00e1 poss\u00edvel ver o erro na tela.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Na KingHost, para ativar essas configura\u00e7\u00f5es para sua aplica\u00e7\u00e3o, basta seguir o passo a passo:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">acessar o Painel de Controle;<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">selecionar o seu Dom\u00ednio;<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">configura\u00e7\u00e3o PHP;<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">ativar display_errors.<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">Quanto ao reporte de erros, adicione o c\u00f3digo error_reporting(E_ALL); no arquivo global de sua aplica\u00e7\u00e3o. Vamos considerar como exemplo o pequeno trecho de c\u00f3digo abaixo:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&lt;?php<\/span><\/p>\n<p><span style=\"font-weight: 400;\">class Cachorro {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0private $raca;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0private $nome;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public function __construct()<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0<\/span> <span style=\"font-weight: 400;\"> $this-&gt;raca = &#8216;Poodle&#8217;;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0<\/span> <span style=\"font-weight: 400;\"> $this-&gt;nome = &#8216;Bob&#8217;;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public function setRaca($raca)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0<\/span> <span style=\"font-weight: 400;\"> $this-&gt;raca = $raca;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public function setNome($nome)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0<\/span> <span style=\"font-weight: 400;\"> $this-&gt;nome == $nome;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public function getInformacoes()<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0<\/span> <span style=\"font-weight: 400;\"> return &#8220;Meu cachorro \u00e9 da ra\u00e7a {$this-&gt;raca} e o nome dele \u00e9 {$this-&gt;nome}&#8221;;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">$cachorro = new Cachorro();<\/span><\/p>\n<p><span style=\"font-weight: 400;\">$cachorro-&gt;setRaca(&#8216;Dogue Alem\u00e3o&#8217;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">$cachorro-&gt;setNome(&#8216;Scooby&#8217;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">echo $cachorro-&gt;getInformacoes();<\/span><\/p>\n<p><span style=\"font-weight: 400;\">O exemplo consiste em uma classe simples chamada Cachorro, que tem as fun\u00e7\u00f5es de setar o nome (setNome) e a ra\u00e7a (setRaca).<\/span><\/p>\n<p><span style=\"font-weight: 400;\">H\u00e1 tamb\u00e9m uma fun\u00e7\u00e3o para disponibilizar as informa\u00e7\u00f5es do cachorro na tela (getInformacoes) e, caso n\u00e3o sejam setadas a ra\u00e7a ou o nome do cachorro, ele tem um construtor para definir os valores padr\u00e3o \u201cPoodle\u201d para ra\u00e7a e \u201cBob\u201d para o nome.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Ao acessar o nosso c\u00f3digo no navegador ou por um terminal veremos que o retorno \u00e9: \u201cMeu cachorro \u00e9 da ra\u00e7a Dogue Alem\u00e3o e o nome dele \u00e9 Bob\u201d. Colocamos o nome Scooby com a fun\u00e7\u00e3o setNome, lembra-se? Ent\u00e3o, vamos aplicar o debug nesta aplica\u00e7\u00e3o.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Sempre temos que pensar em fazer a engenharia reversa, ou seja, quem est\u00e1 aparecendo errado? O nome! Ent\u00e3o vamos primeiro ver qual \u00e9 a fun\u00e7\u00e3o que me retorna as informa\u00e7\u00f5es do cachorro:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0public function getInformacoes()<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0<\/span> <span style=\"font-weight: 400;\"> return &#8220;Meu cachorro \u00e9 da ra\u00e7a {$this-&gt;raca} e o nome dele \u00e9 {$this-&gt;nome}&#8221;;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Temos que entender primeiramente o que esta fun\u00e7\u00e3o b\u00e1sica faz: retorna uma string e, dentro dela, imprime os valores da ra\u00e7a e do nome deste objeto que est\u00e1 instanciado.<\/span><\/p>\n<h2><b>Como usar o var_dump() no PHP?<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">O var_dump() \u00e9 uma fun\u00e7\u00e3o nativa do <\/span><a href=\"https:\/\/king.host\/blog\/tecnologia\/atualizar-versao-php\/\"><span style=\"font-weight: 400;\">PHP<\/span><\/a><span style=\"font-weight: 400;\"> que exibe informa\u00e7\u00f5es detalhadas sobre uma vari\u00e1vel, incluindo seu tipo e valor. Por exemplo, ao aplicar var_dump($variavel);, o PHP retorna detalhes como o tipo da vari\u00e1vel (inteiro, string, array, etc.) e seu conte\u00fado.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Essa fun\u00e7\u00e3o \u00e9 especialmente \u00fatil para visualizar o estado das vari\u00e1veis em diferentes pontos do c\u00f3digo, o que ajuda a identificar onde algo pode estar dando errado.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Retomando o nosso exemplo, se o nome est\u00e1 sendo impresso de forma errada, vamos matar a aplica\u00e7\u00e3o, antes de retornar a string completo e pedir para mostrar somente o valor do nome:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">public function getInformacoes()<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0<\/span> <span style=\"font-weight: 400;\"> var_dump($this-&gt;nome);exit;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0<\/span> <span style=\"font-weight: 400;\"> return &#8220;Meu cachorro \u00e9 da ra\u00e7a {$this-&gt;raca} e o nome dele \u00e9 {$this-&gt;nome}&#8221;;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">A fun\u00e7\u00e3o var_dump, se traduzirmos livremente, \u201cdespejar vari\u00e1vel\u201d, faz exatamente isso: nos mostra todas as informa\u00e7\u00f5es salvas dentro desta vari\u00e1vel. J\u00e1 a fun\u00e7\u00e3o exit, mata o script e n\u00e3o deixa mais nada ser executado. Ent\u00e3o, se acessarmos a aplica\u00e7\u00e3o novamente, teremos o resultado impresso:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">string(3) &#8220;Bob&#8221;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Se o nome est\u00e1 sendo impresso corretamente e n\u00e3o est\u00e1 sendo alterado antes de retornar \u00e0 string, ent\u00e3o temos que analisar onde est\u00e1 sendo setado este nome. Para isso, apague o var_dump e exit da fun\u00e7\u00e3o getInformacoes() e vamos analisar a fun\u00e7\u00e3o setNome():<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public function setNome($nome)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0<\/span> <span style=\"font-weight: 400;\"> $this-&gt;nome == $nome;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Primeiramente vamos verificar se estamos recebendo o par\u00e2metro correto:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">public function setNome($nome)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0<\/span> <span style=\"font-weight: 400;\"> var_dump($nome);exit;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0<\/span> <span style=\"font-weight: 400;\"> $this-&gt;nome == $nome;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">O resultado da aplica\u00e7\u00e3o:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">string(6) &#8220;Scooby&#8221;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Estamos recebendo corretamente o valor do par\u00e2metro, ent\u00e3o, conseguimos ver que o erro est\u00e1 no momento em que atribu\u00edmos este valor \u00e0 propriedade nome do objeto:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">public function setNome($nome)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0<\/span> <span style=\"font-weight: 400;\"> $this-&gt;nome == $nome;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Encontramos o erro! Nesta fun\u00e7\u00e3o, estamos utilizando o s\u00edmbolo de compara\u00e7\u00e3o (==), comparando a propriedade nome do objeto instanciado, cujo valor padr\u00e3o \u00e9 \u2018Bob\u2019, com o que estamos recebendo por par\u00e2metro. Para solucionar, basta arrumarmos isto na fun\u00e7\u00e3o:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0public function setNome($nome)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0<\/span> <span style=\"font-weight: 400;\"> $this-&gt;nome = $nome;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">E, ao acessarmos, teremos o resultado esperado:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&#8220;Meu cachorro \u00e9 da ra\u00e7a Dogue Alem\u00e3o e o nome dele \u00e9 Scooby&#8221;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Este caso \u00e9 simples, mas a t\u00e9cnica de var_dump + exit pode ser utilizada para investigar qualquer tipo de aplica\u00e7\u00e3o.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Vamos para outro caso:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">class Cachorro {<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0private $raca;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0private $nome;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public function __construct()<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0<\/span> <span style=\"font-weight: 400;\"> $this-&gt;raca = &#8216;Poodle&#8217;;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0<\/span> <span style=\"font-weight: 400;\"> $this-&gt;nome = &#8216;Bob&#8217;;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public function setRaca($raca)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0<\/span> <span style=\"font-weight: 400;\"> $this-&gt;raca = $raca;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public function setNome($nome)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0<\/span> <span style=\"font-weight: 400;\"> $this-&gt;nome = $nome;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public function getInformacoes()<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0<\/span> <span style=\"font-weight: 400;\"> return &#8220;Meu cachorro \u00e9 da ra\u00e7a {$this-&gt;raca} e o nome dele \u00e9 {$this-&gt;nome}&#8221;;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">$cachorro = new Cachorro();<\/span><\/p>\n<p><span style=\"font-weight: 400;\">$cachorro-&gt;setRaca(&#8216;Dogue Alem\u00e3o&#8217;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">$cachorro-&gt;setName(&#8216;Scooby&#8217;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">echo $cachorro-&gt;getInformacoes();<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Ao acessarmos este script:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Uncaught Error: Call to undefined method Cachorro::setName()<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Podemos continuar utilizando a fun\u00e7\u00e3o \u2018echo\u2019 para imprimir uma mensagem de qual passo da aplica\u00e7\u00e3o estamos e ir matando ela at\u00e9 encontrar onde est\u00e1 o erro. Por exemplo:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">$cachorro = new Cachorro();<\/span><\/p>\n<p><span style=\"font-weight: 400;\">echo &#8216;Instanciei um objeto cachorro&#8217;;exit;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">$cachorro-&gt;setRaca(&#8216;Dogue Alem\u00e3o&#8217;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">$cachorro-&gt;setName(&#8216;Scooby&#8217;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">echo $cachorro-&gt;getInformacoes();<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Ao acessarmos a aplica\u00e7\u00e3o, veremos que a mensagem \u201cInstanciei um objeto cachorro\u201d \u00e9 mostrada, indicando que n\u00e3o \u00e9 um erro na hora de instanciar o objeto. Continuando:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">$cachorro = new Cachorro();<\/span><\/p>\n<p><span style=\"font-weight: 400;\">$cachorro-&gt;setRaca(&#8216;Dogue Alem\u00e3o&#8217;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">die(&#8216;Informei a raca do meu cachorro&#8217;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">$cachorro-&gt;setName(&#8216;Scooby&#8217;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">echo $cachorro-&gt;getInformacoes();<\/span><\/p>\n<p><span style=\"font-weight: 400;\">A fun\u00e7\u00e3o <\/span><i><span style=\"font-weight: 400;\">die<\/span><\/i><span style=\"font-weight: 400;\">, matar em ingl\u00eas, \u00e9 a jun\u00e7\u00e3o da fun\u00e7\u00e3o echo com exit. Basta passar como par\u00e2metro a mensagem que queremos disponibilizar, a mensagem \u00e9 mostrada e o script morto. Ao acessarmos a aplica\u00e7\u00e3o, veremos a mensagem \u201cInformei a ra\u00e7a do meu cachorro\u201d. Mais uma vez:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">$cachorro = new Cachorro();<\/span><\/p>\n<p><span style=\"font-weight: 400;\">$cachorro-&gt;setRaca(&#8216;Dogue Alem\u00e3o&#8217;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">$cachorro-&gt;setName(&#8216;Scooby&#8217;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">die(&#8216;setei o nome do cachorro&#8217;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">echo $cachorro-&gt;getInformacoes();<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Neste momento, se acessarmos a aplica\u00e7\u00e3o, veremos novamente:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Call to undefined method Cachorro::setName()<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Ent\u00e3o, conseguimos identificar que o erro est\u00e1 no nome da fun\u00e7\u00e3o. Como diz o erro, \u201cCall to undefined method Cachorro::setName()\u201d, Chamada para um m\u00e9todo n\u00e3o definido Cachorro::setName(), pois o nome da fun\u00e7\u00e3o na verdade \u00e9 setNome().<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Alterando o nome da fun\u00e7\u00e3o, a aplica\u00e7\u00e3o volta a funcionar normalmente, e o mesmo procedimento pode ser utilizado para depurar aplica\u00e7\u00f5es que utilizam algum framework MVC, algum <\/span><a href=\"https:\/\/king.host\/blog\/glossario\/o-que-e-cms-php\/\"><span style=\"font-weight: 400;\">CMS<\/span><\/a><span style=\"font-weight: 400;\"> como <\/span><a href=\"https:\/\/king.host\/blog\/casos-de-sucesso\/por-que-escolher-wordpress\/\"><span style=\"font-weight: 400;\">WordPress<\/span><\/a><span style=\"font-weight: 400;\"> ou <\/span><a href=\"https:\/\/king.host\/blog\/tutoriais\/magento-como-configurar-emails\/\"><span style=\"font-weight: 400;\">Magento<\/span><\/a><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Apenas com essas 4 fun\u00e7\u00f5es \u2018echo\u2019, \u2018var_dump\u2019, \u2018exit\u2019, \u2018die\u2019, conseguimos entender o erro em qualquer aplica\u00e7\u00e3o PHP.<\/span><\/p>\n<h2><b>Como utilizar o Xdebug para depura\u00e7\u00e3o avan\u00e7ada no PHP?<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">Embora o var_dump() seja uma ferramenta poderosa para depura\u00e7\u00f5es r\u00e1pidas, ele tem suas limita\u00e7\u00f5es, especialmente em projetos maiores e mais complexos. O Xdebug \u00e9 uma extens\u00e3o do PHP que oferece funcionalidades avan\u00e7adas, como:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">rastreamento de pilha;<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">profiling;<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">capacidade de interromper a execu\u00e7\u00e3o do c\u00f3digo em pontos espec\u00edficos para inspecionar vari\u00e1veis.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Seguindo nosso exemplo, caso precisemos ver os dados da vari\u00e1vel $cachorro como ele \u00e9 instanciado e depois utilizar o var_dump para ver os dados do cachorro ap\u00f3s setar um nome, eu fa\u00e7o dessa maneira no c\u00f3digo:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">PHP<\/span><\/p>\n<p><span style=\"font-weight: 400;\">$cachorro = new Cachorro();<\/span><\/p>\n<p><span style=\"font-weight: 400;\">var_dump($cachorro);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">$cachorro-&gt;setNome(&#8216;Scooby&#8217;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">var_dump($cachorro);exit;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">O retorno deste c\u00f3digo, sem ter o Xdebug habilitado, ser\u00e1 este:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">PHP<\/span><\/p>\n<p><span style=\"font-weight: 400;\">object(Cachorro)#1 (2) { [&#8220;raca&#8221;:&#8221;Cachorro&#8221;:private]=&gt; string(6) &#8220;Poodle&#8221; [&#8220;nome&#8221;:&#8221;Cachorro&#8221;:private]=&gt; string(3) &#8220;Bob&#8221; } object(Cachorro)#1 (2) { [&#8220;raca&#8221;:&#8221;Cachorro&#8221;:private]=&gt; string(6) &#8220;Poodle&#8221; [&#8220;nome&#8221;:&#8221;Cachorro&#8221;:private]=&gt; string(6) &#8220;Scooby&#8221; }<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Ou seja, sem quebras de linha entre os objetos ou entre as propriedades do objeto, deixando mais dif\u00edcil de ler este retorno.<\/span><\/p>\n<h3><b>Como habilitar o Xdebug?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Agora, vamos habilitar o Xdebug. Para isso, \u00e9 preciso baixar a extens\u00e3o para o PHP instalado em sua m\u00e1quina ou acessar o <\/span><a href=\"https:\/\/xdebug.org\/docs\/install\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">site oficial do Xdebug<\/span><\/a><span style=\"font-weight: 400;\"> para encontrar as instru\u00e7\u00f5es de instala\u00e7\u00e3o de acordo com seu Sistema Operacional.<\/span><\/p>\n<h4><b>Como utilizar Xdebug em Linux?<\/b><\/h4>\n<p><span style=\"font-weight: 400;\">Em Linux, basta instalar pelo terminal da sua m\u00e1quina:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">ShellScript<\/span><\/p>\n<p><span style=\"font-weight: 400;\">sudo apt-get install php-xdebug<\/span><\/p>\n<h4><b>Como utilizar Xdebug no Windows?<\/b><\/h4>\n<p><span style=\"font-weight: 400;\">Em Windows, \u00e9 preciso baixar a dll, colocar na pasta de extens\u00f5es e adicion\u00e1-lo pelo php.ini:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Conte\u00fado php.ini<\/span><\/p>\n<p><span style=\"font-weight: 400;\">zend_extension=&#8221;c:\\caminho\\para\\sua\\pasta\\de\\extensoes\\xdebug.dll&#8221;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">xdebug.remote_enable=1<\/span><\/p>\n<p><span style=\"font-weight: 400;\">xdebug.remote_port=9000<\/span><\/p>\n<h4><b>Como utilizar Xdebug em MacOSx?<\/b><\/h4>\n<p><span style=\"font-weight: 400;\">Em MacOSx:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">pecl<\/span><\/p>\n<p><span style=\"font-weight: 400;\">pecl install xdebug<\/span><\/p>\n<h3><b>O que fazer ap\u00f3s a instala\u00e7\u00e3o do Xdebug?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Agora, com Xdebug instalado, acesse novamente sua p\u00e1gina e veja a sa\u00edda do var_dump formatada com quebras de linha e cores para melhor visualiza\u00e7\u00e3o:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">PHP (xdebug_var_dump.png)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\/caminho\/para\/seu\/script\/index.php:37:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">object(Cachorro)[1]<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0private &#8216;raca&#8217; =&gt; string &#8216;Poodle&#8217; (length=6)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0private &#8216;nome&#8217; =&gt; string &#8216;Bob&#8217; (length=3)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\/caminho\/para\/seu\/script\/index.php:39:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">object(Cachorro)[1]<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0private &#8216;raca&#8217; =&gt; string &#8216;Poodle&#8217; (length=6)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0private &#8216;nome&#8217; =&gt; string &#8216;Scooby&#8217; (length=6)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Por \u00faltimo, tamb\u00e9m podemos utilizar a pr\u00f3pria IDE para nos ajudar a debugar a aplica\u00e7\u00e3o, assim como as IDE\u2019s de linguagens compiladas. Vamos tomar o PHPStorm como exemplo, mas, primeiramente, devemos garantir que ele esteja conseguindo se comunicar com o Xdebug. Para isso, v\u00e1 em:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">File;<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Settings;<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Languages &amp; Frameworks;<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">PHP;<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Debug.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">A IDE costuma vir configurada corretamente, mas, para testar, clique em \u201c<\/span><i><span style=\"font-weight: 400;\">Validate debugger configuration on the Web Server<\/span><\/i><span style=\"font-weight: 400;\">\u201d. Em \u201c<\/span><i><span style=\"font-weight: 400;\">Path to create validation script<\/span><\/i><span style=\"font-weight: 400;\">\u201d, coloque o caminho para ser gerado o script e em \u201c<\/span><i><span style=\"font-weight: 400;\">Url to validation script<\/span><\/i><span style=\"font-weight: 400;\">\u201d, a url para acess\u00e1-lo:<\/span><\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-18361 size-large\" src=\"https:\/\/cdn-cms.king.host\/blog-hlg\/uploads\/2018\/06\/validate_php_storm-780x486.png\" alt=\"\" width=\"780\" height=\"486\" title=\"\" srcset=\"https:\/\/cdn-cms.king.host\/blog-hlg\/uploads\/2018\/06\/validate_php_storm-780x486.png 780w, https:\/\/cdn-cms.king.host\/blog-hlg\/uploads\/2018\/06\/validate_php_storm-300x187.png 300w, https:\/\/cdn-cms.king.host\/blog-hlg\/uploads\/2018\/06\/validate_php_storm-768x478.png 768w, https:\/\/cdn-cms.king.host\/blog-hlg\/uploads\/2018\/06\/validate_php_storm-400x249.png 400w, https:\/\/cdn-cms.king.host\/blog-hlg\/uploads\/2018\/06\/validate_php_storm.png 1015w\" sizes=\"(max-width: 780px) 100vw, 780px\" \/><\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-18362 size-full\" src=\"https:\/\/cdn-cms.king.host\/blog-hlg\/uploads\/2018\/06\/validate_script.png\" alt=\"\" width=\"551\" height=\"748\" title=\"\" srcset=\"https:\/\/cdn-cms.king.host\/blog-hlg\/uploads\/2018\/06\/validate_script.png 551w, https:\/\/cdn-cms.king.host\/blog-hlg\/uploads\/2018\/06\/validate_script-221x300.png 221w, https:\/\/cdn-cms.king.host\/blog-hlg\/uploads\/2018\/06\/validate_script-400x543.png 400w\" sizes=\"(max-width: 551px) 100vw, 551px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">Ap\u00f3s garantir que a IDE consegue conversar com o Xdebug, baixe a extens\u00e3o para o seu navegador conversar com a IDE e prossiga com a valida\u00e7\u00e3o. No PHPStorm, marque at\u00e9 qual linha desejas fazer a depura\u00e7\u00e3o clicando no n\u00famero da linha:<\/span><\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-18363 size-full\" src=\"https:\/\/cdn-cms.king.host\/blog-hlg\/uploads\/2018\/06\/linha_marcada.png\" alt=\"\" width=\"696\" height=\"547\" title=\"\" srcset=\"https:\/\/cdn-cms.king.host\/blog-hlg\/uploads\/2018\/06\/linha_marcada.png 696w, https:\/\/cdn-cms.king.host\/blog-hlg\/uploads\/2018\/06\/linha_marcada-300x236.png 300w, https:\/\/cdn-cms.king.host\/blog-hlg\/uploads\/2018\/06\/linha_marcada-400x314.png 400w\" sizes=\"(max-width: 696px) 100vw, 696px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">No seu navegador, ative a extens\u00e3o e acesse o seu script de teste, o que far\u00e1 com que voc\u00ea seja imediatamente redirecionado para o seu PHPStorm e veja o passo a passo do que est\u00e1 sendo feito:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-18364 size-full\" src=\"https:\/\/cdn-cms.king.host\/blog-hlg\/uploads\/2018\/06\/passo_a_passo.png\" alt=\"\" width=\"717\" height=\"544\" title=\"\" srcset=\"https:\/\/cdn-cms.king.host\/blog-hlg\/uploads\/2018\/06\/passo_a_passo.png 717w, https:\/\/cdn-cms.king.host\/blog-hlg\/uploads\/2018\/06\/passo_a_passo-300x228.png 300w, https:\/\/cdn-cms.king.host\/blog-hlg\/uploads\/2018\/06\/passo_a_passo-400x303.png 400w\" sizes=\"(max-width: 717px) 100vw, 717px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">Esse coment\u00e1rios em cinza s\u00e3o gerados pela pr\u00f3pria IDE se comunicando com o Xdebug, e com isso, conseguimos entender que na fun\u00e7\u00e3o setNome est\u00e1 sendo recebido o valor \u201cScooby\u201c, mas, a propriedade nome continua como \u201cBob\u201c.<\/span><\/p>\n<p><b><i>Leia tamb\u00e9m: <\/i><\/b><a href=\"https:\/\/king.host\/blog\/tecnologia\/php-8-novidades-e-impactos-no-desenvolvimento-web\/\"><b><i>PHP 8 \u2014 Novidades E Impactos No Desenvolvimento Web<\/i><\/b><\/a><\/p>\n<p><span style=\"font-weight: 400;\">Como vimos at\u00e9 aqui, existem muitas abordagens para depurar uma aplica\u00e7\u00e3o em PHP, combinando o var_dump() com Xdebug, com uma IDE, ou apenas utilizando-o no script. De qualquer forma, fazer a depura\u00e7\u00e3o em PHP \u00e9 simples e \u00e1gil.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Quer obter resultados ainda melhores com seu site? <\/span><a href=\"https:\/\/king.host\/hospedagem-de-sites\"><span style=\"font-weight: 400;\">Conhe\u00e7a os planos de hospedagem da KingHost<\/span><\/a><span style=\"font-weight: 400;\"> e garanta um ambiente seguro e otimizado para suas aplica\u00e7\u00f5es web.<\/span><\/p>\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped\"><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Leia o artigo e entenda como utilizar as ferramentas de depura\u00e7\u00e3o var_dump() e Xdebug no PHP para identificar e corrigir erros no seu c\u00f3digo.<\/p>\n","protected":false},"author":277,"featured_media":18342,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[156],"tags":[1502,1374],"class_list":["post-18327","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutoriais","tag-debugging-com-var_dump-e-xdebug-no-php","tag-php"],"_links":{"self":[{"href":"https:\/\/king.host\/blog\/wp-json\/wp\/v2\/posts\/18327","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/king.host\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/king.host\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/king.host\/blog\/wp-json\/wp\/v2\/users\/277"}],"replies":[{"embeddable":true,"href":"https:\/\/king.host\/blog\/wp-json\/wp\/v2\/comments?post=18327"}],"version-history":[{"count":15,"href":"https:\/\/king.host\/blog\/wp-json\/wp\/v2\/posts\/18327\/revisions"}],"predecessor-version":[{"id":41713,"href":"https:\/\/king.host\/blog\/wp-json\/wp\/v2\/posts\/18327\/revisions\/41713"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/king.host\/blog\/wp-json\/wp\/v2\/media\/18342"}],"wp:attachment":[{"href":"https:\/\/king.host\/blog\/wp-json\/wp\/v2\/media?parent=18327"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/king.host\/blog\/wp-json\/wp\/v2\/categories?post=18327"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/king.host\/blog\/wp-json\/wp\/v2\/tags?post=18327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}