Skip to content

macianito/js-import-php

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JS PHP Import imports php functions and classes to javascript environment thus they can be accessed as javascript functions and methods of a class

Table of contents

Environments in which to use JS PHP Import

  • Web Browser

Requires

  • PHP
  • Javascript browser support

Usage

Download the code that is in the php_server folder of the project and include it in your HTML.

<script type="text/javascript" src="path_to_php_server/?app=path_to_app"></script>

The url paramameter(variable) app is optional, and it allows to set the path to the application that will be loaded. If not provided, the default application will be loaded

You can use the $exported_fns array to define which functions you want to load in your application. Define it in your application code or in the file settings.php

  • To include functions, use the key 'fns' and an array with the funcion's names as the associated value.

  • To include a class and its methods use name of the class as the key and an array with the method's names as the value of the key.

  // Example

  $exported_fns = array(
    'fns'      => array_merge($defined_functions['internal'], ['name_function1','name_function2']), // functions
    'Foo'      => get_methods_from_class('Foo') // class methods example
  );

Also you can use the $not_allowed_fns array to don't allow whatever functions you put in it.

Examples

We have several examples on the website. Here are some examples to get you started: These examples will put the result of each called php function into a container on the page.

$scandir(PATH_TO_FOLDER).then(function(result) {
  resultObj.html(result.join('<br>'));
});

$system('ls -l').then(function(result) {
  resultObj.append(result);
});

$str_replace(' ').then(function(result) {
  resultObj.append(result);
});

You can call a php function using the result of a previous called php function as a parameter, thus concatenating several function calls. Here is an example of this.

$testfn(34, 56).exec(function(result) { // first call
   return $testfn(34, result); // second call call with the result of the first call as a parameter
}).exec(function(result) {
   resultObj.append('result: ' + result + '<br><br>');
});

Live Example

http://phplandia.org/php_server/index.html

License

JS PHP Import is MIT licensed.

Author

Ivan Macià

Contact Me

About

Import php functions and classes to javascript browser environment

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published