1. Go to this page and download the library: Download leedavis81/altr-ego 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/ */
leedavis81 / altr-ego example snippets
// Given the following class (nice and private all round)
class Foo
{
private $priv = 'This is a private variable';
private $privArray = array('private array entry');
private function privFunc($value)
{
return $value;
}
private static function privStatFunc($value)
{
return $value;
}
}
// We first off create an alter ego
$alterEgo = AltrEgo::create(new Foo());
// Right, now lets get a hook on those private bits
echo $alterEgo->priv . PHP_EOL;
// Output: This is a private variable
// Woot, works, now lets set it to something else and see what happens
$alterEgo->priv = 'new private value';
echo $alterEgo->priv . PHP_EOL;
// Output: new private value
// This value remains on your object, even after retrieving it back with $alterEgo->getObject()
// now lets try some method calls
echo $alterEgo->privFunc('Private call') . PHP_EOL;
//Output: Private call
// Now then, onto arrays; You can pass in an array of parameters like so:
var_dump($alterEgo->privFunc('Private', 'call'));
/**
Output:
array(2) {
[0]=>
string(7) "Private"
[1]=>
string(4) "call"
}
*/
// You can push values straight into them using standard PHP array syntax.
// But be aware, the array will be converted (and maintained) as an ArrayObject
$alterEgo->privArray[] = 'new value';
var_dump($alterEgo->privArray);
/**
Output:
object(ArrayObject)#5 (1) {
["storage":"ArrayObject":private]=>
array(2) {
[0]=>
string(19) "private array entry"
[1]=>
string(9) "new value"
}
}
*/
// You can add associative array values and unset them as you normally would in PHP
$alterEgo->privArray['assoc_key'] = 'private key value entry';
var_dump($alterEgo->privArray);
unset($alterEgo->privArray['assoc_key']);
var_dump($alterEgo->privArray);
/**
Output:
object(ArrayObject)#5 (1) {
["storage":"ArrayObject":private]=>
array(3) {
[0]=>
string(19) "private array entry"
[1]=>
string(9) "new value"
["assoc_key"]=>
string(23) "private key value entry"
}
}
object(ArrayObject)#5 (1) {
["storage":"ArrayObject":private]=>
array(2) {
[0]=>
string(19) "private array entry"
[1]=>
string(9) "new value"
}
}
*/
// We also have the facility to execute static function that have private/protected visibility.
// It's rare you'll ever come across these, they're typically set as public so you'd normally call them directly
echo AltrEgo::callStatic($alterEgo, 'privStatFunc', 'private static call') . PHP_EOL;
// Output: private static call
// Also works with arrays
var_dump(AltrEgo::callStatic($alterEgo, 'privStatFunc', array('private', 'static', 'call')));
/**
Output:
array(3) {
[0]=>
string(7) "private"
[1]=>
string(6) "static"
[2]=>
string(4) "call"
}
*/
// If at anytime you want to jump back into scope just fetch your object back.
// You can throw it back into AltrEgo::create() whenever you need
$backToScope = $alterEgo->getObject();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.