PHP code example of phery / phery

1. Go to this page and download the library: Download phery/phery 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/ */

    

phery / phery example snippets



    ry\Phery;
	Phery::instance()
	->set(array(
		'function_name' => function($data){
				return
					PheryResponse::factory()
						->jquery('<div/>', array('text' => 'text content'))
						->appendTo('body')
						->call('func', 'list', true);
		}
	))->process();


Phery::instance($config)->(...);


	function pre_function_one($ajax_data, $callback_specific_data, $phery_instance)
	{
		// Trim the data, assuming every item is a string, and not an array of array
		return array_map('trim', $ajax_data);
	}

	function pre_function_two($ajax_data, $callback_specific_data, $phery_instance)
	{
		// Remove empty
		return array_filter($ajax_data);
	}

	function post_function($ajax_data, $callback_specific_data, $answer, $phery_instance)
	{
		Database::insert('table', $ajax_data); // key/value pairs for e.g.
		// $answer can be a PheryResponse
		if ($answer instanceof PheryResponse)
		{
			$answer->alert('Ive been post processed!');
		}
		return true;
	}

	Phery::instance()->callback(array(
		'before' => array('pre_function_one', 'pre_function_two'),
		'after' => array('post_function')
	));


	Phery::instance()
	->set(array(
		'delete' => 'process_delete'
	))
	->callback(array(
		'before' => 'callback_function'
	))
	->data(1, 'string', array('named'=>'argument'), new myClass)
	->process();

	function callback_function($ajax_data, $parameters)
	{
		// Since we are not using named parameters, it needs to be accessed through ordinal indexes
		if ($parameters[0] === 1)
		{
			$ajax_data['new_stuff'] = $parameters[3]->execute($parameters[1]);
		}
		return $ajax_data;
	}

	function process_delete($delete, $parameters)
	{
		$id = process($delete['new_stuff']);
		$parameters[3]->clear();
		Database::delete($id);
		return PheryResponse::factory()->alert('Deleted');
	}


	if(Phery::is_ajax()){
		var_dump($_POST);
	} elseif (Phery::is_ajax(true)){
		Phery::respond(PheryResponse::factory());
		exit;
	}

function my_own_error_handler(){
	Phery::respond(PheryResponse::factory()->exception('ERROR!'));
	exit;
}


	Phery::instance()->views(array(
		'#container' => function($data, $params){
			ob_start();
			switch ($_GET['page']){
				case 'home':
				case 'about':
				case 'services':
					


	$phery = Phery::instance();
	$phery->set(array('function' => array('class', 'funct')));
	$phery->process(false);
	// ...
	// continue execution
	$phery->set(array(
		'function2' => array('class' => 'funct2')
	))
	->callback(array(
		'post' => 'function'
	))
	->process();
	// PHP 'exit' is called, from this point and beyond won't get executed unless it doesnt map to any AJAX calls
	$i += 10;


	function outside($ajax_data, $callback_data){
		return PheryResponse::factory();
	}

	class classy {
		function inside($ajax_data, $callback_data){
			return PheryResponse::factory();
		}

		static function inside_static($ajax_data, $callback_data){
			return PheryResponse::factory();
		}
	}

	$class = new classy();

	Phery::instance()->set(array(
		'alias' => function(){
			return PheryResponse::factory();
		},
		'outside' => 'outside',
		'class' => array($class, 'inside'),
		'class' => 'classy::inside_static',
		'namespaced' => 'namespaced\function'
	));


	function func($ajax_data, $callback_data, $phery_instance){
		// $ajax_data = data coming from browser, via AJAX
		//
		// $callback_data = can have anything you specify, plus additional information, like **submit_id** that
		// comes automatically from the AJAX request, containing the ID of the calling DOM element, if has an id="" set
		//
		// $phery_instance = the current instance of Phery
		//
		return PheryResponse::factory(); // In most cases, you'll want to return a PheryResponse object
	}

<head>
	 echo Phery::instance()->csrf(); 


	Phery::factory(array('exceptions' => true))
	->set(array('alias' => 'func'))
	->process();

 echo Phery::link_to('link title', 'function_name', array('class' => 'red', 'href' => '/url')); 

 echo Phery::link_to('link title', 'function_name', array('class' => 'red', 'href' => '/url'), null, true); 

 echo Phery::form_for('/url-to-action/or/empty-means-current-url', 'function_name', array('class' => 'form', 'id' => 'form_id', 'submit' => array('disabled' => true, 'all' => true))) 

 echo Phery::select_for('function_name', array(1 => 'true', 2 => 'hello', 3 => 'control'), array('selected' => 2)) 

 echo Phery::coalesce($undeclared_variable, UNDECLARED_CONSTANT, $data['no-index'], 10) // return 10 


	function func($data, $params, $phery)
	{
		$user = ORM::factory('user', $data['id'])
		->values($data)
		->update();

		return
			PheryResponse::factory('#name')
			/* Set the HTML of the #name */
			->html('<p>'.$user->name.'</p>')
			->show('fast')
			/* Call a jQuery plugin, same as $('.slider').slider(...)  */
			->jquery('.slider')
			->slider(array(
				'value' => 60,
				'orientation' => "horizontal",
				'range' => "min",
				'animate' => true
			))
			/* Creating elements on-the-fly, same $('<p/>') */
			->jquery('<p/>', array('class' => 'attention', 'text' => 'Attention!'))
			->appendTo('header .messages')
			/* Nesting responses */
			->add(PheryResponse::factory('p')->effect('highlight'))
			/* Apply to both p that was created and existing p's on the page */
			->addClass('changed');
	}

PheryFunction::factory('function(){ alert(":msg"); }')->bind(':msg', $msg);



function remote()
{
	return
		PheryResponse::factory()
		->this
		->animate(
			array('opacity' => 0.3),
			1500,
			PheryFunction::factory('function(){ alert("done animating!"); }')
		);
}

Phery::instance()->set(array(
	'remote' => 'remote'
))->process();

 echo Phery::link_to('Click me', 'function', array('args' => ('id' => 1, 'append' => true)); 


Phery::instance()->views(array(
	'#container' => function ($data, $param){
		$render = pseudo_controller();

		return
			PheryResponse::factory()
				->render_view($render['html'] . $param['menu'], array('title' => $render['title']));
	}
))->process();

html
<a data-phery-args="<?=Phery::args(array('id' => 1));
js
phery.remotes([
	['function',{args:1}], //same as phery.remote('function', {args: 1});
	['function2'], // same as phery.remote('function2');
	['function3', null, {target:'/target'}] // same as phery.remote('function3', null, {target: '/target'});
]);
 echo Phery::instance()->csrf(); 
 Phery::link_to('Clicky', 'function', array('only' => true)); 
 PheryResponse::factory()->inline_load(true);