PHP Classes

Learn How to Implement a PHP Object to JSON Converter from the Package PHP Exemplo JSON: Show how to serialize an object in JSON format

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-08-23 (2 days ago) RSS 2.0 feedNot yet rated by the usersTotal: 18 This week: 18All time: 11,307 This week: 4Up
Version License PHP version Categories
php-exemplo-json 1.0GNU General Publi...8Language, PHP 8
Description 

Author

This package can show how to serialize an object in JSON format.

It provides a class to store information about a product.

The class has a function to generate a JSON string from an array with the important class variables.

Picture of Marcelo Telles
  Performance   Level  
Name: Marcelo Telles <contact>
Classes: 5 packages by
Country: Brazil Brazil
Innovation award
Innovation award
Nominee: 2x

Example

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <?php
       
require 'Produto.php';
       
$produto = new Produto("Teclado",150.0);
        echo
$produto->getDados();

        echo
"<hr>";
        echo
"json_encode<br>";
        echo
json_encode($produto,JSON_UNESCAPED_UNICODE);

        echo
"<hr>";
        echo
$produto->converte();

        echo
"<hr>";
       
$dados = get_object_vars($produto);
       
var_dump($dados);

        echo
"<hr>";
       
var_dump($produto);
   
?>
</body>
</html>


Details

Neste exemplo vamos converter os atributos de uma classe usado o json_encode e sobre escrever para personalizar o método json_encode.


  Files folder image Files (3)  
File Role Description
Accessible without login Plain text file index.php Example Example script
Plain text file Produto.php Class Class source
Accessible without login Plain text file README.md Doc. Documentation

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:18
This week:18
All time:11,307
This week:4Up