Trait是PHP 5.4引入的新概念,看上去既像类又像接口,其实都不是,Trait可以看做类的部分实现,可以混入一个或多个现有的PHP类中,其作用有两个:表明类可以做什么;提供模块化实现。Trait是一种代码复用技术,为PHP的单继承限制提供了一套灵活的代码复用机制。 为什么使用Trait PHP语言使用一种典型的单继承模型,在这种模型中,我们先编写一个通用的根类,实现基本的功能,然后扩展这个根类,创建更具体的子类,直接从父类继承实现。这叫做继承层次结构,很多编程语言都使用这个模式。大多数时候这种典型的继承模型能够良好运作,但是如果想让两个无关的PHP类具有类似的行为,应该怎么做呢? Trait就是为了解决这种问题而诞生的。Trait能够把模块化的实现方式注入多个无关的类中,从而提高代码复用,符合DRY(Don’t Repeat Yourself)原则。比如Laravel底层用户认证相关逻辑以及软删除实现等地方都使用了Trait来实现。以Laravel自带的AuthController为例,其中的登录、注册以及登录失败尝试次数都是通过Trait实现: 如何创建Trait 创建Trait很简单,跟创建类有点类似,只不过使用的关键字是trait而不是class,以上述ThrottlesLogin为例: 我们通过trait声明定义的是一个Trait,然后我们可以在这个Trait中像类一样定义要使用的属性和方法。 此外Trait支持嵌套和组合,即通过一个或多个Trait(多个用,分隔)组合成一个Trait,比如AuthenticatesAndRegistersUsers即是如此: 使用多个Trait可能会引起命名冲突问题,上面的代码给出了解决方案:使用insteadof关键字,如果AuthenticatesUsers和RegistersUsers中都定义了redirectPath和getGuard方法,那么将从AuthenticatesUsers中获取对应方法而不是RegistersUsers。另外还可以使用as关键字为方法起个别名,这样也可以避免命名冲突。 此外,这里可能没有完整列出,Trait中还支持定义抽象方法和静态方法,其中抽象方法必须在使用它的类中实现。 这里还需要声明的一点是调用方法的优先级:调用类>Trait>父类(如果有的话),方法可以覆盖,但属性不行,如果Trait中定义了一个属性,如果调用类中也定义这个属性则会报错。 如何使用Trait Trait的使用方法也很简单,上面已经显示的很清楚明了,即使用use关键字。 可能你已经注意到,命名空间和Trait使用的都是use关键字,不同之处在于导入位置,命名空间在类的定义体外导入,而Trait在类的定义体内导入。 from:http://laravelacademy.org/post/4281.html
View Details接口不是现代PHP的新特性,但是非常重要,学会使用接口,可以极大提升我们的编程能力,所以在日常开发中应该尽可能多地使用接口。 接口是两个PHP对象之间的契约(Contract),Laravel底层就直接将接口放在Contracts目录中: 接口将我们的代码和依赖解耦了,而且允许我们的代码依赖任何实现了预期接口的第三方代码,我们不管第三方代码是如何实现接口的,只关心第三方代码是否实现了指定的接口。 如果我们编写的代码需要处理指定类的对象,那么代码的功用就完全被限定了,因为始终只能使用那个类的对象,可是,如果编写的代码处理的是接口,那么代码立即就能知道如何处理实现这一接口的任何对象,我们不关心接口是如何实现的,只关心是否实现了指定的接口。 我们以上述Laravel底层提供的CacheStore(Store接口)为例,这个接口的作用是封装缓存存储器的通用方法,包括get、put、flush等:
|
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 90 91 92 |
<?php namespace Illuminate\Contracts\Cache; interface Store { /** * Retrieve an item from the cache by key. * * @param string|array $key * @return mixed */ public function get($key); /** * Retrieve multiple items from the cache by key. * * Items not found in the cache will have a null value. * * @param array $keys * @return array */ public function many(array $keys); /** * Store an item in the cache for a given number of minutes. * * @param string $key * @param mixed $value * @param int $minutes * @return void */ public function put($key, $value, $minutes); /** * Store multiple items in the cache for a given number of minutes. * * @param array $values * @param int $minutes * @return void */ public function putMany(array $values, $minutes); /** * Increment the value of an item in the cache. * * @param string $key * @param mixed $value * @return int|bool */ public function increment($key, $value = 1); /** * Decrement the value of an item in the cache. * * @param string $key * @param mixed $value * @return int|bool */ public function decrement($key, $value = 1); /** * Store an item in the cache indefinitely. * * @param string $key * @param mixed $value * @return void */ public function forever($key, $value); /** * Remove an item from the cache. * * @param string $key * @return bool */ public function forget($key); /** * Remove all items from the cache. * * @return void */ public function flush(); /** * Get the cache key prefix. * * @return string */ public function getPrefix(); } |
这么做的好处是可以分开定义具体的缓存实现方式,比如Laravel支持数组(Array)、数据库(Database)、文件(File)、Apc、Memcache、Redis等缓存存储器,方便我们在代码中使用相应的方式对数据进行缓存。我们以Memcached驱动为例,其对应实现类是MemcachedStore:
|
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
<?php namespace Illuminate\Cache; use Memcached; use Illuminate\Contracts\Cache\Store; class MemcachedStore extends TaggableStore implements Store { /** * The Memcached instance. * * @var \Memcached */ protected $memcached; /** * A string that should be prepended to keys. * * @var string */ protected $prefix; /** * Create a new Memcached store. * * @param \Memcached $memcached * @param string $prefix * @return void */ public function __construct($memcached, $prefix = '') { $this->setPrefix($prefix); $this->memcached = $memcached; } /** * Retrieve an item from the cache by key. * * @param string|array $key * @return mixed */ public function get($key) { $value = $this->memcached->get($this->prefix.$key); if ($this->memcached->getResultCode() == 0) { return $value; } } /** * Retrieve multiple items from the cache by key. * * Items not found in the cache will have a null value. * * @param array $keys * @return array */ public function many(array $keys) { $prefixedKeys = array_map(function ($key) { return $this->prefix.$key; }, $keys); $values = $this->memcached->getMulti($prefixedKeys, null, Memcached::GET_PRESERVE_ORDER); if ($this->memcached->getResultCode() != 0) { return array_fill_keys($keys, null); } return array_combine($keys, $values); } /** * Store an item in the cache for a given number of minutes. * * @param string $key * @param mixed $value * @param int $minutes * @return void */ public function put($key, $value, $minutes) { $this->memcached->set($this->prefix.$key, $value, $minutes * 60); } /** * Store multiple items in the cache for a given number of minutes. * * @param array $values * @param int $minutes * @return void */ public function putMany(array $values, $minutes) { $prefixedValues = []; foreach ($values as $key => $value) { $prefixedValues[$this->prefix.$key] = $value; } $this->memcached->setMulti($prefixedValues, $minutes * 60); } /** * Store an item in the cache if the key doesn't exist. * * @param string $key * @param mixed $value * @param int $minutes * @return bool */ public function add($key, $value, $minutes) { return $this->memcached->add($this->prefix.$key, $value, $minutes * 60); } /** * Increment the value of an item in the cache. * * @param string $key * @param mixed $value * @return int|bool */ public function increment($key, $value = 1) { return $this->memcached->increment($this->prefix.$key, $value); } /** * Decrement the value of an item in the cache. * * @param string $key * @param mixed $value * @return int|bool */ public function decrement($key, $value = 1) { return $this->memcached->decrement($this->prefix.$key, $value); } /** * Store an item in the cache indefinitely. * * @param string $key * @param mixed $value * @return void */ public function forever($key, $value) { $this->put($key, $value, 0); } /** * Remove an item from the cache. * * @param string $key * @return bool */ public function forget($key) { return $this->memcached->delete($this->prefix.$key); } /** * Remove all items from the cache. * * @return void */ public function flush() { $this->memcached->flush(); } /** * Get the underlying Memcached connection. * * @return \Memcached */ public function getMemcached() { return $this->memcached; } /** * Get the cache key prefix. * * @return string */ public function getPrefix() { return $this->prefix; } /** * Set the cache key prefix. * * @param string $prefix * @return void */ public function setPrefix($prefix) { $this->prefix = ! empty($prefix) ? $prefix.':' : ''; } } |
可以看到我们在构造函数中传入了Memcached实例,然后在此实例基础上具体实现接口所定义的方法,其他的实现类也是类似,这样通过Store接口,我们就将缓存代码和具体依赖解耦,方便后续扩展以及供其他人使用。比如这里我们定义一个CacheStore类:
|
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 |
<?php namespace App\Tests; use Illuminate\Contracts\Cache\Store; class CacheStore { protected $store; public function __construct(Store $store) { $this->store = $store; } public function get($key) { return $this->store->get($key); } public function put($key, $value, $minutes=1) { $this->store->put($key, $value, $minutes); } public function forget($key) { $this->store->forever($key); } public function flush() { $this->store->flush(); } } |
然后我们可以在配置文件中配置使用的默认缓存驱动,比如Memcached,然后在代码中调用时这样使用:
|
1 2 3 4 5 6 |
$memcached = new \Memcached(); $memcached->addServer('localhost',11211); $memcachedCache = new MemcachedStore($memcached); $cacheStore = new CacheStore($memcachedCache); $cacheStore->put('site','<a href="http://laravelacademy.org/">http://LaravelAcademy.org</a>'); dd($cacheStore->get('site')); |
注:这里只是做简单演示,不要真的这么去使用Laravel提供的缓存功能,实际上Laravel底层对缓存处理要比我这里的演示代码优雅的多。 总之,使用接口编写的代码更灵活,能委托别人实现细节,使用接口后会有越来越多的人使用你的代码,因为他们只需要知道如何实现接口,就可以无缝地使用你的代码。实际上,我们在使用服务提供者和依赖注入时也是以这种面向接口编程为基础进行了更复杂的扩展而已。 from:http://laravelacademy.org/post/4246.html
View Details1、什么是命名空间 如果你只需要知道现代PHP特性中的一个,那就应该是命名空间。命名空间在PHP5.3.0中引入,其作用是按照一种虚拟的层次结构组织PHP代码,这种层次结构类似操作系统中文件系统的目录结构。命名空间是现代PHP组件生态的基础,现代的PHP组件框架代码都是放在各自全局唯一的厂商命名空间中,以免和其他厂商使用的常见类名冲突。 下面我来看看真实的PHP组件是如何使用命名空间的。Laravel框架中的Http组件用于管理HTTP请求和响应,这个组件用到了常见的类名,例如Request、Response,很多其他PHP组件也用到了这样的类名,既然其他PHP代码也用到了相同的类名,那怎么使用这个组件呢?其实我们可以放心使用,因为这个组件的代码放在了唯一的厂商命名空间Illuminate中。打开这个组件在GitHub中的仓库(https://github.com/laravel/framework/blob/master/src/Illuminate/Http/Response.php),找到Response.php文件: 第3行代码如下:
|
1 |
namespace Illuminate\Http; |
这一行是PHP命名空间声明语句,声明命名空间的代码始终应该放在<?php标签后的第一行。通过这个命名空间的声明语句我们可以看到Response位于厂商命名空间Illuminate中(最顶层命名空间),我们还看到Response类在子命名空间Http中,你可以看下和Response.php文件在同一层级的其他文件,会发现它们都使用相同的命名空间声明语句。 命名空间的作用是封装和组织相关的PHP类,就像在文件系统中把相关的文件放在同一个目录中一样。PHP命名空间和操作系统的物理文件系统不同,这是一个虚拟概念,没必要和文件系统中的目录结构完全相同,虽然如此,但是大多数PHP组件为了兼容广泛使用的PSR-4自动加载标准,会把命名空间放到对应文件系统的子目录中。 2、为什么使用命名空间 前面已经提到过,我们的代码可能和其他开发者的代码使用相同的类名、接口名、函数或常量名,如果不使用命名空间,名称会起冲突,导致PHP执行出错。而使用命名空间将代码放到唯一的厂商命名空间,我们的代码就可以和其他开发者使用相同的类名、接口名、函数或常量名。 当然如果你开发的是小型个人项目,只有少量的依赖,类名冲突可能不是问题,但是如果在团队中工作,开发用到许多第三方依赖的大型项目,就要认真对待命名冲突问题,因为你无法控制项目依赖在全局命名空间中引入的类、接口、函数和常量,这也是为什么要使用命名空间的原因。 3、声明命名空间 每个PHP类、接口、函数和常量都在命名空间中,声明命名空间很简单,在<?php标签后的第一行声明,声明语句以namespace开头,随后是一个空格,然后是命名空间的名称,最后以;结尾。 命名空间经常用于设置顶层厂商名,比如我们设置厂商名为LaravelAcademy:
|
1 2 |
<?php namespace LaravelAcademy; |
在这个命名空间声明语句后声明的所有PHP类、接口、函数和常量都在LaravelAcademy命名空间中,而且和Laravel学院有某种关系。如果我们想组织学院用到的代码该怎么做呢?答案是使用子命名空间。 子命名空间的声明方式和前面的示例完全一样,唯一的区别是我们要使用\符号把命名空间和子命名空间分开,例如:
|
1 2 |
<?php namespace LaravelAcademy\ModernPHP; |
这个命名空间后的所有类、接口、函数和常量都位于LaravelAcademy\ModernPHP中。 在同一个命名空间中的类没必要在同一个PHP文件中声明,可以在PHP文件的顶部指定一个命名空间或子命名空间,此时,这个文件的代码就是该命名空间或子命名空间的一部分。因此我们可以在不同文件中编写属于同一个命名空间的多个类。 注:厂商命名空间是最顶层的命名空间,也是最重要的命名空间,用于识别品牌或组织,必须具有全局唯一性。子命名空间相对而言没那么重要,但是可以用于组织项目的代码。 4、导入和别名 在命名空间出现之前,PHP开发者使用Zend风格的类名解决命名冲突问题,这是一种类的命名方案,因Zend框架而流行,这种命名方案在PHP类名中使用下划线的方式表示文件系统的目录分隔符。这种约定有两个作用:其一,确保类名是唯一的;其二,原生的自动加载器会把类名中的下划线替换成文件系统的目录分隔符,从而确定文件的路径。例如,Zend_Cloud_DocumentService_Adapter_WindowsAzure_Query类对应的文件是Zend/Cloud/DocumentService/Adapter/WindowsAzure/Query.php。可以看出,这种命名有个缺点:类名特别长。 现代的PHP命名空间也有这个问题,例如上述Response类完整的全名是Illuminate\Http\Response,幸好,我们可以通过导入以及创建别名的方式来改变这一状况。 导入的意思是指,在每个PHP文件中告诉PHP想使用哪个命名空间、类、接口、函数和常量,导入后就不用使用全名了:
|
1 2 3 4 5 |
<?php use Illuminate\Http\Response; $response = new Response(‘Oops’, 400); $response->send(); |
我们通过use关键字告诉PHP,我们想使用Illuminate\Http\Response类,我们只需要输入一次完全限定的类名,随后实例化Response的时候,无需使用完整的类名。 如果觉得这样的类名还是长,可以创建别名。创建别名指的是告诉PHP我要使用简单的名称引用导入的类、接口、函数或常量:
|
1 2 3 4 5 6 |
<?php use Illuminate\Http\Response as Res; $res = new Res(‘Oops’, 400); $res->send(); |
从PHP 5.6开始还可以导入函数和常量,不过要调整use关键字的句法,如果要导入函数,需要使用use func:
|
1 2 3 4 |
<?php use func Namespace\functionName functionName(); |
如果想导入常量,可以使用use constant:
|
1 2 3 4 |
<?php use constant Namespace\CONST_NAME; echo CONST_NAME; |
当然也支持别名,创建方式和类一样。 5、实用技巧 多重导入 如果想要在PHP文件中导入多个类、接口、函数或常量,需要在PHP文件的顶部使用多个use语句,PHP支持用简短的语法把多个use语句写成一行:
|
1 2 3 |
<?php use Illuminate\Http\Request, Illuminate\Http\Response; |
但是为了可读性,建议不要这么写,还是一行写一个use语句比较好:
|
1 2 3 |
<?php use Illuminate\Http\Request; use Illuminate\Http\Response; |
一个文件使用多个命名空间 PHP允许在一个文件中定义多个命名空间:
|
1 2 3 4 5 6 7 8 |
<?php namespace Foo { //声明类、接口、函数、常量 } namespace Bar { //声明类、接口、函数、常量 } |
但这么做不好,违背了“一个文件一个类”的良好实践,因此不建议这么做。 全局命名空间 如果引用的类、接口、函数和常量没有指定命名空间,PHP假定引用的类、接口、函数和常量在当前的命名空间中。如果要使用其他命名空间的类、接口、函数或常量,需要使用完全限定的PHP类名(命名空间+类名)。 有些代码在全局命名空间中,没有命名空间,比如原生的Exception类就是这样。在命名空间中引用全局的代码时,需要在类、接口、函数或常量前加\符号:
|
1 2 3 4 5 6 7 8 |
<?php namespace My\App; class Foo { public function doSomething() { throw new \Exception(); } } |
自动加载 命名空间还为PHP-FIG制定的PSR-4自动加载标准奠定了坚实的基础,大多数现代的PHP组件都使用了这种自动加载模式,使用依赖管理器Composer可以自动加载项目的依赖,后续我们还会详细介绍Composer和PHP-FIG,现在你只需要知道没有命名空间,就没有现代的PHP生态系统和基于组件的新型架构,由此可见命名空间的重要性。 from:http://laravelacademy.org/post/4221.html
View Details常用设置: 去掉波浪线: settings -> Editor -> Colors & Fonts -> General -> TYPO->Effects 显示行号: settings -> Editor->Appearance->Show line numbers 去掉右上角浏览器图标: settings -> tools -> WebBrowsers 启动的时候不打开工程文件 Settings->General去掉Reopen last project on startup. 取消自动保存 appearance -> system settings -> save file的两个选项 去掉 常用快捷键: ctrl + f : 查找 ctrl + shift + f :查找任意内容,这个功能叼叼的~~ ctrl + r : 查找替换 ctrl + n : 查找类名称 ctrl + shift + n : 查找文件名称 ctrl + alt + shift + n : 查找方法名称 shift + shift :查找任意文件名称 ctrl + b: 跳到变量申明处 ctrl + […]
View Detailscomposer install命令出错-->
|
1 2 3 |
[Composer\Downloader\TransportException] Your configuration does not allow connections to http://packagist.phpcomposer.com/packages.json. See https://getcom poser.org/doc/06-config.md#secure-http for details. |
解决方法:因为镜像使用用的是http,而原地址是需要https,所以配置下关掉https就好了。
|
1 2 3 4 5 |
{ "config": { "secure-http": false } } |
报错信息:
|
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 |
Warning: Accessing packagist.phpcomposer.com over http which is an insecure protocol. Updating dependencies (including require-dev) Your requirements could not be resolved to an installable set of packages. Problem 1 - laravel/framework v5.3.9 requires ext-openssl * -> the requested PHP extension openssl is missing from your system. - laravel/framework v5.3.8 requires ext-openssl * -> the requested PHP extension openssl is missing from your system. - laravel/framework v5.3.7 requires ext-openssl * -> the requested PHP extension openssl is missing from your system. - laravel/framework v5.3.6 requires ext-openssl * -> the requested PHP extension openssl is missing from your system. - laravel/framework v5.3.5 requires ext-openssl * -> the requested PHP extension openssl is missing from your system. - laravel/framework v5.3.4 requires ext-openssl * -> the requested PHP extension openssl is missing from your system. - laravel/framework v5.3.3 requires ext-openssl * -> the requested PHP extension openssl is missing from your system. - laravel/framework v5.3.2 requires ext-openssl * -> the requested PHP extension openssl is missing from your system. - laravel/framework v5.3.18 requires ext-openssl * -> the requested PHP extension openssl is missing from your system. - laravel/framework v5.3.17 requires ext-openssl * -> the requested PHP extension openssl is missing from your system. - laravel/framework v5.3.16 requires ext-openssl * -> the requested PHP extension openssl is missing from your system. - laravel/framework v5.3.15 requires ext-openssl * -> the requested PHP extension openssl is missing from your system. - laravel/framework v5.3.14 requires ext-openssl * -> the requested PHP extension openssl is missing from your system. - laravel/framework v5.3.13 requires ext-openssl * -> the requested PHP extension openssl is missing from your system. - laravel/framework v5.3.12 requires ext-openssl * -> the requested PHP extension openssl is missing from your system. - laravel/framework v5.3.11 requires ext-openssl * -> the requested PHP extension openssl is missing from your system. - laravel/framework v5.3.10 requires ext-openssl * -> the requested PHP extension openssl is missing from your system. - laravel/framework v5.3.1 requires ext-openssl * -> the requested PHP extension openssl is missing from your system. - laravel/framework v5.3.0 requires ext-openssl * -> the requested PHP extension openssl is missing from your system. - Installation request for laravel/framework 5.3.* -> satisfiable by laravel/framework[v5.3.0, v5.3.1, v5.3.10, v5.3.11, v5.3.12, v5.3.13, v5.3.14, v5.3.15, v5.3.16, v5.3.17, v5.3.18, v5.3.2, v5.3.3, v5.3.4, v5.3.5, v5.3.6, v5.3.7, v5.3.8, v5.3.9]. |
解决方案:修改php.ini开启openssl拓展 Laravel 出现"RuntimeException inEncrypter.php line 43: The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths."问题的解决办法——> 首先在cmd命令行下定位到项目所在的根目录下,接着输入:
|
1 |
php artisan key:generate |
这时候项目根目录下的.env文件里的APP_KEY应该会有值了: 若没有,将上面的key输入进去 from:http://www.cnblogs.com/vincebye/p/5947739.html
View Details(PHP 4 >= 4.0.4, PHP 5, PHP 7) call_user_func_array — 调用回调函数,并把一个数组参数作为回调函数的参数 说明 mixed call_user_func_array ( callable $callback , array $param_arr ) 把第一个参数作为回调函数(callback)调用,把参数数组作(param_arr)为回调函数的的参数传入。 参数 callback 被调用的回调函数。 param_arr 要被传入回调函数的数组,这个数组得是索引数组。 返回值 返回回调函数的结果。如果出错的话就返回FALSE 更新日志 版本 说明 5.3.0 对面向对象里面的关键字的解析有所增强。在此之前,使用两个冒号来连接一个类和里面的一个方法,把它作为参数来作为回调函数的话,将会发出一个E_STRICT的警告,因为这个传入的参数被视为静态方法。 范例 Example #1 call_user_func_array()例子 <?php function foobar($arg, $arg2) { echo __FUNCTION__, " got $arg and $arg2\n"; } class foo { function bar($arg, $arg2) { echo __METHOD__, " got $arg and $arg2\n"; } } // Call the foobar() function with 2 arguments call_user_func_array("foobar", array("one", "two")); // Call the $foo->bar() method with 2 arguments $foo = new foo; call_user_func_array(array($foo, "bar"), array("three", "four")); ?> 以上例程的输出类似于:
|
1 2 |
foobar got one and two foo::bar got three and four |
Example #2 call_user_func_array()使用命名空间的情况 <?php namespace Foobar; class Foo { static public function test($name) { print "Hello {$name}!\n"; } } // As of PHP 5.3.0 call_user_func_array(__NAMESPACE__ .’\Foo::test', array('Hannes')); // As of PHP 5.3.0 call_user_func_array(array(__NAMESPACE__ .’\Foo', 'test'), array('Philip')); ?> 以上例程的输出类似于:
|
1 2 |
Hello Hannes! Hello Philip! |
Example #3 把完整的函数作为回调传入call_user_func_array() <?php $func = function($arg1, $arg2) { return $arg1 * $arg2; }; var_dump(call_user_func_array($func, array(2, 4))); /* As of PHP 5.3.0 */ ?> 以上例程会输出:
|
1 |
int(8) |
Example #4 传引用 <?php function mega(&$a){ […]
View Details本文档仅介绍如何安装iWshop并且完成微信公众号对接,其他微信认证、微信支付等申请可以参考: 百度经验 微信认证申请、微信支付申请 iWshop 交流群:470442221 iWshop 类文档:http://docs.ycchen.cc/iwshop/index.html 一、准备工作 服务器环境要求 PHP5.3+ MySQL 5.5.3+ (utf8mb4编码用于保存带有emoji表情的微信用户昵称) PHP扩展:php_mysql php_curl php_pdo_mysql php_mcrypt php_gd2 请确保您的php.ini配置中magic_quotes_gpc为Off,否则某些功能可能无法使用 如果需要redis,请加载php_redis扩展 iWshop是以UrlQuery的形式组合参数的,所以不需要伪静态模块。 开始安装iWshop http://git.oschina.net/koodo/iWshop/releases 现在以安装目录F:\dev_project\test_iwshop为例进行安装,我的httpd-vhosts.conf其中的一个设置是这样的。关于如何使用apache的vhosts可以参考:Apache官方示例 1 2 3 4 5 6 7 8 9 10 <VirtualHost *:80> DocumentRoot "F:\dev_project\test_iwshop" ServerName test.iw.com <Directory "F:\dev_project\test_iwshop"> AllowOverride All Options FollowSymLinks Order allow,deny Allow from all </Directory> </VirtualHost> 当然你也可以直接放服务器根目录。 然后在浏览器中打开localhost,或者vhosts指向的域名。 填写微店名称(可以在后台修改),数据库密码,后台管理员账号密码(可以在后台修改),然后点击下一步。 这里特别说明一下系统根目录,如果是在DocumentRoot的根目录下安装,那么就是 / ,如果是在某个子目录比如/iw/,那么这里就要填写 /iw/ ,一般情况下都会自动获取,无需填写,如果遇到css或者js等静态文件无法加载页面错乱的问题,请检查config.php里面的docroot选项。 点击马上安装,如果数据库版本和php环境没有什么配置问题的话,就安装成功了。 假设服务器域名是:www.iwshop.cn,那么你的: 后台地址:http://www.iwshop.cn/?/Wdmin/login/ 微信消息接口地址:http://www.iwshop.cn/wechat/ 二、微信对接 // todo 三、bug反馈 http://git.oschina.net/koodo/iWshop/issues iWshop 交流群:470442221 作者邮箱 koodo@qq.com from:http://git.oschina.net/koodo/iWshop/blob/dev/html/docs/install.md
View Details[摘要:远期正在进修yii2框架的应用,正在看他人的专客时讲到了好化url的完成 比方,默许进进about页里的时间,url是如许的http://localhost/index.php?r=site%2Fabout,只需正在/config/web.p] 近期在学习yii2框架的使用,在看别人的博客时讲到了美化url的实现 比如,默认进入about页面的时候,url是这样的http://localhost/index.php?r=site%2Fabout,只要在/config/web.php中,在components数组中加上 urlManager => [ showScriptName => false, enablePrettyUrl => true ], 开启之后再访问about页面,路径已变为http://localhost/web/site/about 但是我在本机上使用的时候,一直报404错误,搜索过后才知道是因为apache没有开启rewrite功能 最后找到了这个博客 http://www.chenruixuan.com/archives/759.html yii basic版: 1. 开启 apache 的 mod_rewrite 模块 去掉LoadModule rewrite_module modules/mod_rewrite.so前的“#”符号; 2. 修改 apache 的 AllowOverride 把 AllowOverride None 修改为 AllowOverride All; 3. 在与index.php文件同级目录(web目录)下添加文件“.htaccess” Options +FollowSymLinks IndexIgnore */* RewriteEngine on # if a directory or a file exists, use it directly RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # otherwise forward it to index.php RewriteRule . index.php 4. 配置应用的urlManager yii2.0与之前版本配置略有不同,根据文档显示: http://www.yiiframework.com/doc-2.0/yii-web-urlmanager.html 需要在config/web.php中的components数组下增加: 1 2 3 4 5 6 7 8 […]
View Details|
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 |
<?php /** * Created by PhpStorm. * User: w3cnet * Date: 2016/12/10 * Time: 22:50 */ namespace app\models; class AESCrypt{ /** * 设置默认的加密key * @var str */ private static $key = "A9823E9B32A658F72814D0B612F550EC"; /** * 设置默认加密向量 * @var str */ private static $iv = 'A9823E9B@#A658F72814D0^&12F550E$'; /** * 设置加密算法 * @var str */ private static $cipher = MCRYPT_RIJNDAEL_256; /** * 设置加密模式 * @var str */ private static $mode = MCRYPT_MODE_CBC; /** * 对内容加密,注意此加密方法中先对内容使用padding pkcs7,然后再加密。 * @param str $content 需要加密的内容 * @return str 加密后的密文 */ public static function encrypt($content){ if(empty($content)){ return null; } $src_data = $content; $block_size = mcrypt_get_block_size(self::$cipher, self::$mode); $padding_char = $block_size - (strlen($content) % $block_size); $src_data .= str_repeat(chr($padding_char),$padding_char); return base64_encode(mcrypt_encrypt(self::$cipher, self::$key, $src_data, self::$mode, self::$iv)); } /** * 对内容解密,注意此加密方法中先对内容解密。再对解密的内容使用padding pkcs7去除特殊字符。 * @param String $content 需要解密的内容 * @return String 解密后的内容 */ public static function decrypt($content){ if(empty($content)){ return null; } $content = base64_decode($content); $content = mcrypt_decrypt(self::$cipher, self::$key, $content, self::$mode, self::$iv); $pad = ord($content[($len = strlen($content)) - 1]); return substr($content, 0, strlen($content) - $pad); } } |
View Details