PHP code example of harm-less / php-encoder
1. Go to this page and download the library: Download harm-less/php-encoder library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
harm-less / php-encoder example snippets
PE\Encoders\XmlEncoder;
use PE\Nodes\EncoderNode;
use PE\Nodes\EncoderNodeVariable;
// create a simple class with 1 variable and a setter/getter
class HelloWorld {
private $foo;
public function setFoo($value) {
$this->foo = $value;
}
public function getFoo() {
return $this->foo;
}
}
// create a corresponding node and add the variable
class HelloWorldNode extends EncoderNode {
function __construct() {
parent::__construct('hello-worlds', 'hello-world', null);
$this->addVariable(new EncoderNodeVariable('foo'));
}
}
// register the node so it becomes known to the encoder
EncoderNode::addNode(new HelloWorldNode());
// create a HelloWorld object
$helloWorld = new HelloWorld();
$helloWorld->setFoo('hello world');
// make an instance of an encoder type and encode the object
$encoder = new XmlEncoder();
$encodedResultXml = $encoder->encode($helloWorld);
// will output:
/* <?xml version="1.0" encoding="UTF-8"