PHP代码打包---phar

php中的phar类似于java中的打包文件jar,即将一个文件夹中的一类文件压缩。

功能:Phar可以将一组PHP文件进行打包,还可以创建默认执行的stub(或者叫做 bootstraploader)。
前提: 将php.ini中phar.readonly 设置为0/false;
创建过程: user目录下有3个文件,其中test.php是默认入口文件。

user.class.php代码如下:

class User{
    private $name="lyf3312";
    private $email="lyf3312@163.com";
 
    public function set_email($email) {
        $this->email=$email;
    }
    public function set_name($name) {
        $this->name=$name;
    }
    public function introduce() {
        echo "My name is $this->name and my email address is $this->email.\n";
    } 
}

user.func.php代码如下:
	require_once 'user.class.php';
	
	function make_user($name,$email) {
	    $u=new user();
	    $u->set_name($name);
	    $u->set_email($email);
	    return $u;
	}	 
	function dump_user($u) {
	    $u->introduce();
	}
test.php 代码如下:
	require_once 'user.class.php';

	$u = new User();
	$u->set_name("lyf");
	$u->set_email("lyf@163.com");
	$u->introduce()

打包脚本如下:
$phar = new Phar('user.phar', 0, 'user.phar');
	// 建立压缩目录
	$phar->buildFromDirectory(dirname(__FILE__) . '/user');
	// 压缩php文件的入口文件
	$phar->setStub($phar->createDefaultStub('test.php', 'test.php'));
	// 压缩格式
	$phar->compressFiles(Phar::GZ);

使用:

1. 直接执行  php.exe   /path/user.phar;
2. 请求脚本执行
require_once "user.phar"; //默认执行了user.phar中test.php
	// 指明类的路径
	require_once "phar://user.phar/user.class.php";
	$u=new user();
	$u->set_name("lyf1232");
	$u->set_email("lyf1232@gmail.com");
	$u->introduce();
	echo '<hr>';
	require_once "phar://user.phar/user.func.php";
	 
	$u=make_user("lyf","lyf@163.com");
	dump_user($u);

拆包:
http://unphar.com/cn 这个是在线拆包,直接下载就ok
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
安装ShardingSphere-PHP 1. 下载ShardingSphere-PHP 从ShardingSphere官网下载ShardingSphere-PHP的源代码,解压到Web服务器的根目录下,例如/var/www/html/sharding-sphere-php。 2. 安装Composer Composer是PHP中最流行的依赖管理工具,需要先安装Composer。 可以使用以下命令在Linux环境中安装: ``` curl -sS https://getcomposer.org/installer | php mv composer.phar /usr/local/bin/composer ``` 3. 安装ShardingSphere-PHP依赖 在sharding-sphere-php目录下执行以下命令安装依赖: ``` composer install ``` 配置ShardingSphere-PHP 在ShardingSphere-PHP中,需要配置ShardingSphere-JDBC的数据源和分片规则,然后配置ShardingSphere-PHP的数据源和分片规则。 1. 配置ShardingSphere-JDBC的数据源和分片规则 在ShardingSphere-JDBC的配置文件中,配置数据源和分片规则,例如: ``` # 数据源配置 spring.shardingsphere.datasource.names=ds0,ds1 spring.shardingsphere.datasource.ds0.type=com.zaxxer.hikari.HikariDataSource spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver spring.shardingsphere.datasource.ds0.jdbc-url=jdbc:mysql://localhost:3306/db0?serverTimezone=UTC&useSSL=false spring.shardingsphere.datasource.ds0.username=root spring.shardingsphere.datasource.ds0.password=123456 spring.shardingsphere.datasource.ds1.type=com.zaxxer.hikari.HikariDataSource spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver spring.shardingsphere.datasource.ds1.jdbc-url=jdbc:mysql://localhost:3306/db1?serverTimezone=UTC&useSSL=false spring.shardingsphere.datasource.ds1.username=root spring.shardingsphere.datasource.ds1.password=123456 # 分片规则配置 spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=id spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=ds${id % 2} spring.shardingsphere.sharding.tables.user.actual-data-nodes=ds${0..1}.user spring.shardingsphere.sharding.tables.user.table-strategy.inline.sharding-column=id spring.shardingsphere.sharding.tables.user.table-strategy.inline.algorithm-expression=user${id % 2} ``` 2. 配置ShardingSphere-PHP的数据源和分片规则 在ShardingSphere-PHP的配置文件中,配置数据源和分片规则,例如: ```php // 数据源配置 $shardingDataSourceConfig = new ShardingDataSourceConfiguration(); $shardingDataSourceConfig->getShardingRuleConfiguration()->getTableRuleConfigs()['user'] = new ShardingTableRuleConfiguration('user', 'ds${0..1}.user', 'id', new InlineShardingAlgorithm('id', 'user${id % 2}')); $shardingDataSourceConfig->getShardingRuleConfiguration()->getDefaultDatabaseShardingStrategyConfig() = new InlineShardingStrategyConfiguration('id', 'ds${id % 2}'); $shardingDataSourceConfig->getDataSourceConfiguration()->getDataSourceConfigs()['ds0'] = new DataSourceConfiguration('mysql:host=localhost;port=3306;dbname=db0', 'root', '123456'); $shardingDataSourceConfig->getDataSourceConfiguration()->getDataSourceConfigs()['ds1'] = new DataSourceConfiguration('mysql:host=localhost;port=3306;dbname=db1', 'root', '123456'); // 创建数据源 $shardingDataSource = new ShardingDataSource($shardingDataSourceConfig); ``` 这里的ShardingDataSourceConfiguration和ShardingDataSource是ShardingSphere-PHP提供的类,用于配置和创建ShardingSphere数据源。 使用ShardingSphere-PHP 配置好ShardingSphere-PHP后,就可以使用ShardingSphere-PHP操作分片数据库了。例如: ```php // 插入数据 $statement = $shardingDataSource->getConnection()->prepare('INSERT INTO user (id, name) VALUES (?, ?)'); $statement->bindValue(1, 1); $statement->bindValue(2, 'Alice'); $statement->execute(); // 查询数据 $statement = $shardingDataSource->getConnection()->prepare('SELECT * FROM user WHERE id = ?'); $statement->bindValue(1, 1); $statement->execute(); $row = $statement->fetch(PDO::FETCH_ASSOC); ``` 这里的$shardingDataSource是ShardingSphere-PHP创建的数据源,可以使用PDO API进行操作。注意,在ShardingSphere-PHP中,对于分片的表,需要使用分片键进行操作,否则会出现数据不一致的情况。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值