tags:

views:

51

answers:

2

How can i encrypt a function or its contents in a php class ?

e.g. Take a look at below class, i would like to encrypt the function test1() so the code inside will never be revealed but executes as normal

class test
{
 var $x;
 var $y;

 function test1()
 {
  return $this->x; 

 }

 function test2()
 {
  return $this->y; 

 }

}

Thanks in advance

+3  A: 

The code can't really be hidden from someone that has access to the file, but it can be obfuscated. There are too many alternatives, try google 'php obfuscate'.

Keep in mind that even though it will be harder, it will by no means be impossible for a skilled coder to quickly discern what happens in your code (especially if it's as simple as the example you provided).

nc3b
A: 

Extract test1 to base class:

abstract class testBase {
  function test1(){
    return $this->x;
  }
}

Encode it with http://www.zend.com/en/products/guard/. Then use testBase as test parent:

class test extends testBase {
  public $x;
  public $y;

  function test2(){
    return $this->y;
  }
}
Sam Dark
Thank you sam but my class already have a parent class.. and php doesnt support multiple inheritance.. does it?e.gclass test extends testparent
jane
It does not. But it does support multiple interfaces I believe
webdestroya