调用类私用方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php

class Cat
{
private function want($see)
{
echo $see.$see.$see.PHP_EOL;
}
}

$reflector = new ReflectionClass(Cat::class);
$instance = $reflector->newInstance();
$method = $reflector->getmethod('want');
$method->setAccessible(true);
$result = $method->invoke($instance, 'eat food ');

实例化不可实例的类

使用这个的原因是因为拆分老的业务代码中,有个一个 Aentity 依赖另一个 Bentity (在orm层中调用get_class),如果查询或者Bentity提供的创建方法会导致将其注册到 UnitOfWork 中,会导致最后的入库(虽然也有可能不入库),所以采用 ReflectionClass::newInstanceWithoutConstructor 实例化一个在 get_class 范围内等价的 entity ,并将其作为一个临时的参数对象代替 Bentity 维持代码的运作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
function println(String $str)
{
echo($str.PHP_EOL);
}

class Dog
{
protected $name;

private function __construct($name = 'WangCai')
{
$this->name = $name;
}

public function setName($name)
{
$this->name = $name;
}

public function getName()
{
return $this->name;
}
}

$reflector = new ReflectionClass(Dog::class);
$dog = $reflector->newInstanceWithoutConstructor();
$dog->setName('DeFu');
$class = get_class($dog);
println($class);
println($dog->getName());

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
$i = 1;

$str = <<<EOF
<?php
\$logName = basename(__FILE__);
\$i =1;

\$result = '';
\$result .= "初始: " . memory_get_usage() . "B\n";
\$result .= "使用: " . memory_get_usage() . "B\n";
while(\$i--){
sleep(10);

\$result .= "释放: " . memory_get_usage() . "B\n";
\$result .= "峰值: " . memory_get_peak_usage() . "B\n";
file_put_contents(__DIR__.'/'.\$logName.'.log',time().PHP_EOL.\$result,FILE_APPEND);
}
EOF;
$file_name = [];
while ($i < 10) {
$file_name[] = 'timeEvent' . $i . '.php';
file_put_contents(__DIR__ . '/' . 'timeEvent' . $i . '.php', $str);
$i++;
}

var_dump($file_name);

foreach ($file_name as $name) {

$strRun = 'php ' . __DIR__ . '/' . $name . ' &';
$waitClose = pclose(popen($strRun, 'r'));
echo time() . PHP_EOL;
}
$result = '';
$result .= "初始: " . memory_get_usage() . "B\n";
$result .= "使用: " . memory_get_usage() . "B\n";
$result .= "释放: " . memory_get_usage() . "B\n";
$result .= "峰值: " . memory_get_peak_usage() . "B\n";

1
2
3
4
5
<?php
ob_start();
debug_print_backtrace($needBacktrace === 2 ? 1 : 0);
$trace = ob_get_contents();
ob_end_clean();
1
2
3
4
5
6
7
<?php
define('PROJECT_START', microtime(true));
$message = [
'memory_used' => memory_get_usage(),
'running_time' => (microtime(true) - PROJECT_START),
'action_name' => $actionName
];

1
2
3
4
5
6
7
8
9
10
11
composer create-project symfony/skeleton my-project
cd my-project
composer require symfony/web-server-bundle --dev
composer require annotations # 注释分析
composer require symfony/orm-pack
composer require symfony/maker-bundle --dev
composer require --dev symfony/profiler-pack # 开发调试工具
composer require sensio/framework-extra-bundle # 自动查询
composer require doctrine/doctrine-fixtures-bundle --dev # 测试数据
composer require symfony/messenger
composer require symfony/serializer-pack #待定

起步

1
2
3
4
5
6
#symfony 提供两种项目 symfony/skeleton 是轻量的, symfony/website-skeleton 是比较重的,具有较全的web功能
#选择轻量的面向 api 和 console 的 symfony/skeleton 起步
#使用 composer 安装symfony
composer create-project symfony/skeleton my-project
#使用 symfony-cli 安装symfony
symfony new xz_symfony

symfonyConfiguring a Web Server
symfonyRequirements for Running Symfony
进入项目安装开发时使用的 web server 组件

1
2
cd xz_symfony
composer require symfony/web-server-bundle --dev

1
2
3
4
5
composer require annotations         # 注释分析
php bin/console debug:router # 查看所有的路由
composer require symfony/twig-bundle # symfony 的模板引擎
php bin/console debug:autowiring # 查看所有可以注入的服务

The Symfony MakerBundle

1
2
composer require symfony/maker-bundle --dev          # 引入 maker-bundle
php bin/console make:controller BrandNewController # 创建一个新的控制器

1
php bin/console config:dump-reference framework
1
2
3
4
5
6
composer require symfony/orm-pack
composer require symfony/maker-bundle --dev
php bin/console doctrine:database:create
php bin/console list doctrine
php bin/console make:entity
php bin/console make:migration
1
2
3
4
php bin/console doctrine:query:sql 'SELECT * FROM product'
composer require sensio/framework-extra-bundle # 自动查询
composer require doctrine/doctrine-fixtures-bundle --dev # 测试数据
php bin/console doctrine:fixtures:load

next

1
2
3
4
# 全局唯一id
# fixtures 文件的生成位置
php bin/console make:fixtures

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
运行 Laravel Mix
Laravel Mix 一款前端任务自动化管理工具,使用了工作流的模式对制定好的任务依次执行。Mix 提供了简洁流畅的 API,让你能够为你的 Laravel 应用定义 Webpack 编译任务。Mix 支持许多常见的 CSS 与 JavaScript 预处理器,通过简单的调用,你可以轻松地管理前端资源。

使用 Mix 很简单,首先你需要使用以下命令安装 npm 依赖即可。我们将使用 Yarn 来安装依赖,在这之前,因为国内的网络原因,我们还需为 Yarn 配置安装加速:

$ yarn config set registry https://registry.npm.taobao.org
使用 Yarn 安装依赖:

$ yarn install
安装成功后,运行以下命令即可:

$ npm run watch-poll
watch-poll 会在你的终端里持续运行,监控 resources 文件夹下的资源文件是否有发生改变。在 watch-poll 命令运行的情况下,一旦资源文件发生变化,Webpack 会自动重新编译。

注意:在后面的课程中,我们需要保证 npm run watch-poll 一直处在执行状态中。
1
2
3
4
5
6
7
7. 线上部署须知
在开发环境中,我们为了测试方便,直接在命令行里调用 artisan horizon 进行队列监控。然而在生产环境中,我们需要配置一个进程管理工具来监控 artisan horizon 命令的执行,以便在其意外退出时自动重启。当服务器部署新代码时,需要终止当前 Horizon 主进程,然后通过进程管理工具来重启,从而使用最新的代码。

简而言之,生产环境下使用队列需要注意以下两个问题:

使用 Supervisor 进程工具进行管理,配置和使用请参照 文档 进行配置;
每一次部署代码时,需 artisan horizon:terminate 然后再 artisan horizon 重新加载代码。

more php

虽然 wsl 可以模拟 linux 环境,但是请不要用 wsl 作为一种开发环境进行使用,而是作为一个工具系统

一致的环境

  • linux 版本 & 配置
  • nginx 版本 & 配置
  • mysql 版本 & 配置
  • php 版本 & 配置
  • composer 版本(这个也很重要)

一致的环境体现在三个方面,系统(linux/win)是否一直,依赖软件(nginx/mysql/other)是否一致,解析器(php)版本及该使用该解析器所做的软件(composer)版本是否一致。

1
2
3
composer update
composer install
composer update --no-dev

Deployer 一个环境部署工具

about use

1
2
3
4
5
6
7
if ($needBacktrace != 0) {
ob_start();
debug_print_backtrace($needBacktrace === 2 ? 1 : 0);
$trace = ob_get_contents();
ob_end_clean();
$msg = $msg . '## trace:' . $trace;
}

ubuntu

ubuntu 中软件的卸载方法:

1.apt方式安装:(相应的文件夹有相关的软件,先进入到软件的文件夹) 普通安装: sudo apt-get install 软件名 修复安装: sudo apt-get -f install 软件名 重新安装: sudo apt-get --rreinstall install 软件名  (一般是 .deb 格式) 2.dpkg 方式: 1.普通安装:sudo dpkg -i package_name.deb ===================================== 如果软件的格式为 .rpm 格式包时,则 1.先安装 alien 和 fakeroot 这两个工具, sudo apt-get install alien fakeroot alien 把 .rpm 为 .deb 格式的文件 1. 将 .rpm 格式的文件转为 同文件名的 .deb fakeroot alien xx.rpm 这样就可以按上面的方法安装了

1.apt 方式: a. 移除式卸载: apt-get remove 软件名 b. 清除式卸载: apt-get --purge remove 软件名 (同时清除配置) c. 清除式卸载: apt-get --purge 软件名 (同时清除配置)

2.dpkg 方式: a. 移除式卸载:sudo dpkg dpkg_name b. 清除式卸载:sudo -P dpkg_name

查看已经安装的软件名称:

dpkg -l

查找软件库中的软件 apt-cache search 正则表达式 或者 aptitude search 软件包(部分) 可以查看相关软件的名称 标志 i 表示已经安装

apt-cache search php7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16


./configure \
--prefix=/usr/local/php73 \
--exec-prefix=/usr/local/php73 \
--bindir=/usr/local/php73/bin \
--sbindir=/usr/local/php73/sbin \
--includedir=/usr/local/php73/include \
--libdir=/usr/local/php73/lib/php \
--mandir=/usr/local/php73/php/man \
--with-config-file-path=/usr/local/php73/etc \
--with-openssl \
--enable-mbstring \
--enable-fpm

sudo make && sudo make install

编译

1
2
3
4
5
6
7
8
# php7.1
./configure --prefix=/usr/local/php71 --with-config-file-path=/usr/local/php71/etc --with-config-file-scan-dir=/usr/local/php71/conf.d --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir=/usr/local/freetype --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --disable-fileinfo --enable-intl --with-xsl --enable-maintainer-zts
./configure --with-php-config=/usr/local/php71/bin/php-config --with-readline

# php7废弃的扩展
# --with-mysql=mysqlnd

sudo make && sudo make install
1
2
3
4
5
6
Warning! a PEAR user config file already exists from a previous PEAR installation at '/home/hanzhijie/.pearrc'. You may probably want to remove it.
Wrote PEAR system config file at: /usr/local/php71/etc/pear.conf
You may want to add: /usr/local/php71/lib/php to your php.ini include_path
/mnt/c/Users/hzj/Desktop/boom/tmp/php-7.1.26/build/shtool install -c ext/phar/phar.phar /usr/local/php71/bin
ln -s -f phar.phar /usr/local/php71/bin/phar
Installing PDO headers: /usr/local/php71/include/php/ext/pdo/

php7.3

1
2
3
4
5
6
7


./configure --prefix=/usr/local/php73 --with-config-file-path=/usr/local/php73/etc --with-config-file-scan-dir=/usr/local/php73/conf.d --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir=/usr/local/freetype --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --enable-ftp --with-gd --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --disable-fileinfo --enable-intl --with-xsl --with-kerberos --enable-sysvshm --enable-opcache --enable-libxml --with-bz2 --with-pear --with-pdo-sqlite --with-pcre-regex --with-libdir=lib64 --with-kerberos --enable-maintainer-zts
./configure --with-php-config=/usr/local/php73/bin/php-config --with-readline
# php7.2废弃的扩展
# --with-mcrypt --enable-gd-native-ttf

1
2
3
4
5
6
Warning! a PEAR user config file already exists from a previous PEAR installation at '/home/hanzhijie/.pearrc'. You may probably want to remove it.
Wrote PEAR system config file at: /usr/local/php73/etc/pear.conf
You may want to add: /usr/local/php73/lib/php to your php.ini include_path
/mnt/c/Users/hzj/Desktop/boom/tmp/php-7.3.2/build/shtool install -c ext/phar/phar.phar /usr/local/php73/bin
ln -s -f phar.phar /usr/local/php73/bin/phar
Installing PDO headers: /usr/local/php73/include/php/ext/pdo/

pthreads 安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
----------------------------------------------------------------------
Libraries have been installed in:
/mnt/c/Users/hzj/Desktop/boom/tmp/pthreads-master/modules

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the `LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the `LD_RUN_PATH' environment variable
during linking
- use the `-Wl,--rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to `/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------

Build complete.
Don't forget to run 'make test'.

Installing shared extensions: /usr/local/php73/lib/php/extensions/no-debug-zts-20180731/

https://github.com/slince/composer-registry-manager

1
2
3
4
5
$ composer repo:ls
composer https://packagist.org
phpcomposer https://packagist.phpcomposer.com
* composer-proxy https://packagist.composer-proxy.org
laravel-china https://packagist.laravel-china.org
1
2
3
4
5
6
7
$ composer repo:use
Please select your favorite registry (defaults to composer)
[0] composer
[1] phpcomposer
[2] composer-proxy
[3] laravel-china
>
1
$ composer repo:use phpcomposer

数据结构实验之链表五:单链表的拆分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <cstring>
using namespace std;
struct node
{
int data;
struct node *next;
};

int main ()
{
std::ios::sync_with_stdio(false);

struct node *head , *tail,*p;

head =new node;
head->next = NULL;
tail = head;

int num;
int sum1,sum2;

cin>>num;
while(num--)
{
p = new node ;
cin>>p->data;
tail->next = p;
tail = tail->next;
}
p->next = NULL;


struct node *head1 , *head2 ,*tail1,*tail2;

head1 = new node;
head1->next = NULL;
tail1 = head1;

head2 = new node;
head2->next = NULL;
tail2 =head2;

p = head->next;
sum1 = 0;
sum2 = 0;
while(p!=NULL){

if(p->data%2 ==1){
tail1->next = p;
tail1 = tail1->next;
sum1 +=1;
} else {
tail2->next = p;
tail2 = tail2->next;
sum2 +=1;
}
p = p->next;
}
tail1->next = NULL;
tail2->next = NULL;
cout << sum2 << ' '<<sum1<<endl;

p = head2->next;
while(p!=NULL){
cout<<p->data;
if(p->next!=NULL){
cout<< ' ';
}
p = p->next;

}
cout<<endl;

p = head1->next;
while(p!=NULL){
cout<<p->data;
if(p->next!=NULL){
cout<< ' ';
}
p = p->next;

}
cout<<endl;

return 0;

}

0%