EzDevInfo.com

zephir

Zephir is a compiled high level language aimed to the creation of C-extensions for PHP Zephir Language — Zephir v0.6.3 documentation

Is it possible with Zephir to include an external library?

I have some code in C which does some hardware access. This code is ready and well tested. Now I want to implement a web interface for controlling this hardware. So I came along PHP extension development with Zephir.

My question is, „Is it possible with Zephir to include an external library resp. link against it?“ and if it is possible, how can I do it?


Source: (StackOverflow)

run the zephir language on windows machines

Is it possible to use the zephir language on a windows system when the packages json-c and re2c are required.

Can I somehow install or build the packages in windows to then build and run zephir?


Source: (StackOverflow)

Advertisements

How can I install Zephir on Ubuntu?

I would like to install Zephir on my Ubuntu machine.

The goal is to convert some of my existing code into a PHP extension, in order to get the benefits of faster execution.

Any pointers are more than appreciated.


Source: (StackOverflow)

Zephir Language Parse Exception

I am trying to compile the below code in zephir language and it gives me Parse error. I am not sure what am I doing wrong.

 public static function calculateDiscrepancy(tpImpr, liImpressions, defaults) {
    var numeratorx = 1-(tpImpr + defaults);
    if numeratorx != 0 && liImpressions != 0 {
          return (double)(numeratorx / liImpressions) * 100;
    }else{
    return 0;
    }
}

Error

Zephir\ParseException: Syntax error in /var/www/vhosts/app/advertisingcalculator.zep on line 58

     var numeratorx = 1-(tpImpr + defaults);
-----------------------^

Any Ideas?


Source: (StackOverflow)

Need Help Understanding a Crude Benchmark: Regular PHP vs HHVM vs Zephir

I performed this test using a simple factorial function (borrowed the logic from http://avelino.xxx/2014/03/golang-c-and-python-the-benchmark-time)

Regular PHP Code

function fact($n){ 
    if($n===0)return 1;
    return $n*fact($n-1);
}

function calc(){
    $t = 0;
    for($i=0; $i<100000; $i++){
        for($j=0; $j<8; $j++){
            $t += fact($j);
        }
    }
    return $t; 
}

$result = calc();
echo $result."\n";

PHP Using Zephir

$fact = new Utils\Fact();
$result = $fact->calc();
echo $result."\n";

Zephir Code

namespace Utils;

class Fact{
    public function fact(int n) -> int{
        if(n==0){
            return 1;
        }

        return n*this->fact(n - 1);
    }

    public function calc() -> int{
        int i,j,total;
        let total = 0;
        for i in range(0,99999){
            for j in range (0,7){
                let total = total + this->fact(j);
            }
        }
        return total;
    }
}

I executed these snippets using the time command in the following manner:

Regular PHP

time php -c /etc/php5/apache2/php.ini regular.php

Result

591400000
real 0m0.788s
user 0m0.736s
sys 0m0.026s

PHP Using Zephir Class

time php -c /etc/php5/apache2/php.ini zephyr.php

Result

591400000
real 0m1.529s
user 0m1.494s
sys 0m0.024s

HHVM

time hhvm regular.php

Result

591400000
real 0m0.883s
user 0m0.814s
sys 0m0.045s

Question:

As you can see from the results above, regular PHP code seems to have performed better than the one that uses a compiled Zephyr class as a PHP extension. This is what has me confused.

How can the scripted code end up being faster than the compiled one, especially when both employ the same logic? I think I'm missing something here and would be grateful if someone could help me understand this.

EDIT: Looks like others are facing a similar problem with Zephir: Zephir 2x slower


Source: (StackOverflow)

Using PDO in Zephir

As part of my experiments with Zephir I am currently trying to use PHP PDO to access a MySQL database. For starters I found that a relatively innocuous

$dbh = new PDO("mysql:host=localhost;dbname=dbn;","user","pwd");

when translated and used in Zephir

var dbh = new PDO("mysql:host=localhost;dbname=dbn;","user","pwd");

had Zephir throwing up an exception

var dbh = new PDO
------------^

which by dint of some searching I resolved - new is a reserved word in Zephir and must be replaced with $new.

var dbh = $new PDO("mysql:host=localhost;dbname=dbn;","user","pwd");

which promptly produced

var dbh = $new PDO(
-----------------^

which I resolved when I realized that I had to explicitly tell Zephir to use the PDO name space

use \PDO;
var dbh = $new \PDO::PDO(

Now, with

var dbh = $new \PDO::PDO("mysql:host=localhost;dbname=dbn","user","pwd");

I get

var dbh = $new \PDO::PDO(...,"user","pwd");
---------------------------------------------^

which makes little sense to me.

From what I can tell Zephir is still too young to be considered for a full port of a working PHP prototype. However, it looks like it is good enough to be used to port some of the more CPU intensive bits of a PHP application but its documentation is lacking. For instance, nowhere does it state in the docs that the right way to use an array is

array myArray;
 let myArray = [1,2,...];

Miss out the first list and the compiler complains about not being able to mutate.

With my current PDO problem there is a clearly something else that is wrong but I have no idea what it might be. I'd much appreciate any help.


Source: (StackOverflow)

Multiple class files in a Zephir extension

I'm doing some experiments with Phalcon Zephir to see how well it can convert some of my libraries to PHP extensions.

I have two PHP classes, each already defined in its own file: the Zephir notes are quite clear that this must be the case.

trienode.zep

namespace tries;

class trienode
{
    public children;

    public valueNode = false;

    public value = null;

    public function __construct()
    {
        let this->children = [];
    }
}

and

trie.zep

namespace tries;

class trie {

    private trie;

    public function __construct() {
        let this->trie = new trienode();
    }
}

But whenever I try to compile the classes using zephir compile, I get

Warning: Class "trienode" does not exist at compile time  in /home/vagrant/ext/tries/tries/trie.zep on 8 [nonexistent-class]

            let this->trie = new trienode();
    ---------------------------------------^

(and if I continue through the build process, and install the resultant .so file, it errors when I try to use it from within a PHP script)

<?php

namespace tries;

$test = new trie;

giving

PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/lib/php5/20121212/tries.so' - /usr/lib/php5/20121212/tries.so: undefined symbol: zephir_tries_trie_init in Unknown on line 0
PHP Fatal error:  Class 'tries\trie' not found in /home/vagrant/triesTest.php on line 5

I've looked through the Zephir documentation, and various blog posts, but can't find any examples of building an extension that comprises more than a single class file.

Has anybody succeeded in building an extension using Zephir that does comprise more than a single class? And if so, what settings or configuration options (or additional steps) does it require to build a working so?


Source: (StackOverflow)

run zephir build error on tutorial

I installed zephir OK (0.3.7a) and run the test, and got error as follows:

[root@vmlinux64 test]# zephir build PHP Warning: system() has been disabled for security reasons in /usr/local/lib64/zephir/Library/CompilerFile.php on line 107 PHP Warning: file_get_contents(.temp/0.3.7a/_root_test_test_Hello.zep.js): failed to open stream: No such file or directory in /usr/local/lib64/zephir/Library/CompilerFile.php on line 110 Zephir\Exception: Cannot parse file: /root/test/test/Hello.zep

I run zephir help is ok. 0.3.7a . and I do like this:

$ zephir test
$ cd test/test/
$ vim Hello.zep 

in Hello.zep:

namespace Test;

class Hello
{
public function say()
{
echo "hello from test";
}
}

and save it, then go to ../ and run zephir build

the errors popped as above ...

I installed lnmp-1.0-full on my centOS6.5_x86_64, php version is 5.3.17.

Thanks in advance!


Source: (StackOverflow)

Loading extensions in php5-fpm

I am in the process of experimenting with Zephir on my Nginx/php5-fpm/ubuntu14.04 setup. I followed their tutorial and managed to compile my first Zephir PHP extension with little difficulty. However, when I tried to enable the newly built extension by editing /etc/php5/fpm/php.ini to include

extension=/path/to/test.so

did not show up the test extension upon issuing php -m. I then remembered that to install the mcrypt extension I use php5enmod mcrypt. So I went to /etc/php5/mods-available and created the file test.ini

extension=/path/to/test.so

and then issued a

php5enmod test

A simple

service php5-fpm restart && php -m

later and lo & behold the test extension was present! All very good but I still do not understand how php5enmod does its magic. It clearly is not writing to the php.ini file. I'd be much obliged to anyone who might be able to explain.


Source: (StackOverflow)

Zephir Tutorial Error

I try to apply http://zephir-lang.com/tutorial.html .I run this code in my desktop(text_ext directory).

zephir init utils
cd utils

and zephir build results are:

baris@ubuntu:~/Desktop/test_ext/utils$ zephir build
Preparing for PHP compilation...
Preparing configuration file...
shtool:mkdir:Error: invalid number of arguments (at least 1 expected)
shtool:mkdir:Hint:  run `./build/shtool mkdir -h' or `man shtool' for details
Compiling...
Installing...
Extension installed!
Add extension=utils.so to your php.ini
Don't forget to restart your web server

php -v command

PHP 5.5.8-3+sury.org~saucy+2 (cli) (built: Jan 29 2014 13:30:11) 
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2013 Zend Technologies
    with Zend OPcache v7.0.3-dev, Copyright (c) 1999-2013, by Zend Technologies

In result, "utils.so" is not created.


Source: (StackOverflow)

Zephir on Windows and Linux (ubuntu) - errors

Afters hours and hours spend on trying to install zephir on my windows 8, i finally managed to build an single .zep file !

Now, i've copied the .dll from /ext/Release directory, into my xampp/php/ext and added the extension=.dll in my php.ini - I'm using latest xampp with php 5.5.24 - and when I restart my Apache, the httpd.exe throws an error "The program can't start because php5.dll is missing form your computer". I'm really baffled by this eror, searching on net for the last hours and I can't find an answer !

Does anyone had this issue before ?

Problem 2) I've install vagrant and followed the tutorial from blog, and managed to install everything.Then I went to /vagrant directory as it is shared between the host and the guest, used zaphir init , all the files were generated, then copied my .zep file from my windows host into the right directory and runned zephir build. Again all went fine, the .so file was created but as my main development platform is on windows, i wanted to use the files from /etc/ to build the .dll extension for my xampp installation - to be honest i didn't tried to use the .so extension and test if the extension works on linux - .

Now, using the Developer Command Promt for Visual Studio 2012, i tried to run command "cl .c" inside the /ext/ directory that was build by zaphir on linux, but an fatal error its appearing in my developer command promt "Fatal error C1083: Cannot open include file: php.h: No such file or directory"

The greatest way for me would be to compile the extension for both windows, my dev. pc but for linux also !

Could please someone help me with this issues ? Give me some guidance as I have to admit, I don't have knowledge about developing/compiling in C !

Thank you


Source: (StackOverflow)

Zephir giving error on windows: Installation is not implemented for windows yet

When i build extension with Visual Studio Command Prompt (2010) it gives error:

Cannot load Xdebug - it was built with configuration

API220100525,TS,VC9, whereas running engine is API220100525,NTS,VC9

startPreparing for PHP compilation...

Preparing configuration file...

Compiling...

Installation is not implemented for windows yet! Aborting!

Link to extension+log-files zip: http://modsolutionz.com/utils.zip

Link to error image: http://modsolutionz.com/error1.png


Source: (StackOverflow)

zephir error: when run "zephir build"

I try zephir to compile php lib to c but I get a error below:

   Preparing PHP compilation...
    Preparing configuration file...
    Compiling...
    /var/www/html/mylib/ext/kernal/object.c: 
In function 'zephir_fetch_static_property_re': 
/var/www/html/mylib/ext/kernal/object.c:1377: 
warning: passing argument 2 of 'zend_read_static_property' 
discards qualifiers from pointer target type
    /usr/include/php/Zend/zen_API.h:321: 
note: expected 'char *' but argument is of type 'const char *'
    /var/www/html/mylib/ext/kernal/object.c: 
In function 'zephir_update_static_property_ex':
/var/www/html/mylib/ext/kernal/object.c:1466: 
warning: passing argument 2 of 'zend_std_get_static_property' 
discards qualifiers from pointer target type
    /usr/include/php/Zend/zend_object_handlers.h:147: 
note: expected 'char *' but argument is of type 'const char *'
    Installing...
    Extension installed!
    Add extension=mylib.so to your php.ini
    Don't forget to restart your web server

who can help me to sovle? Thanks advandce,


Source: (StackOverflow)

Zephir Error : Unable to load dynamic library '/usr/lib/php5/20121212/utils.so'

Today, i installed 'Zephir' on my ubuntu machine. After spending some hour(going through zephir docs) with Zephir I noticed 2 things. - The 'config.json' file which suppose to be created inside of "utlis" folder(as of zephir doc) it's getting created outside of "utlis" folder. - After successfully testing 1st example 'Greeting'(as of zephir doc ) when i created the 2nd example of "Filter" Class & tried to build the extension by "zephir build" command i got following error..

Error message : PHP Warning:  PHP Startup: Unable to load dynamic library   '/usr/lib/php5/20121212/utils.so' - /usr/lib/php5/20121212/utils.so: undefined symbol: zephir_Utils_Bark_init in Unknown on line 0

In fact, i feel whenever i tried to create a new Class inside "utlis" folder i got that error. I made some quick google search about the error but didn't get any result.

Guys any idea, what's going wrong... ?


Source: (StackOverflow)

Connect to mysql using Zaphir

How can I connect to my sql using Zephir, also I'm using php, I tried the link but failed.

this is how I use the code.

public function setConnection(connection) -> void
{
    let this->_connection = connection;
}

let myDb = db->setConnection(connection);
myDb->execute("SELECT * FROM robots");

it gives only a vauge idea and throws exception

thanks and regards.


Source: (StackOverflow)