标签: php-7

PHP 7 simpleXML

我正在测试PHP7,并在最近的更新后出现了一个奇怪的问题.默认情况下应该启用SimpleXML,我的phpinfo页面显示它可用:

在此输入图像描述

但是,这些功能不可用:

<?php

if (function_exists('simplexml_load_file')) {
    echo "simpleXML functions are available.<br />\n";
} else {
    echo "simpleXML functions are not available.<br />\n";
}

// result-- NOT available
Run Code Online (Sandbox Code Playgroud)

并且该模块未列为已加载:

~ $ php -m
[PHP Modules]
calendar
Core
ctype
curl
...
Reflection
session
shmop
sockets
SPL
standard
sysvmsg
sysvsem
sysvshm
tokenizer
Zend OPcache
zlib
Run Code Online (Sandbox Code Playgroud)

任何人都知道这是否有解决方法?

版本信息:

~ $ php -v
PHP 7.0.3-8+deb.sury.org~trusty+2 (cli) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies …
Run Code Online (Sandbox Code Playgroud)

php simplexml php-7

63
推荐指数
8
解决办法
13万
查看次数

为什么我在引入属性类型提示时突然收到“在初始化之前不能访问类型化属性”错误?

我已经更新了我的类定义以使用新引入的属性类型提示,如下所示:

class Foo {

    private int $id;
    private ?string $val;
    private DateTimeInterface $createdAt;
    private ?DateTimeInterface $updatedAt;

    public function __construct(int $id) {
        $this->id = $id;
    }


    public function getId(): int { return $this->id; }
    public function getVal(): ?string { return $this->val; }
    public function getCreatedAt(): ?DateTimeInterface { return $this->createdAt; }
    public function getUpdatedAt(): ?DateTimeInterface { return $this->updatedAt; }

    public function setVal(?string $val) { $this->val = $val; }
    public function setCreatedAt(DateTimeInterface $date) { $this->createdAt = $date; }
    public function setUpdatedAt(DateTimeInterface $date) { …
Run Code Online (Sandbox Code Playgroud)

php type-hinting doctrine-orm php-7 php-7.4

62
推荐指数
2
解决办法
5万
查看次数

在PHP 7中键入提示 - 对象数组

也许我错过了一些东西,但有没有选项来定义该函数应该有参数或返回例如User对象的数组?

请考虑以下代码:

<?php

class User
{
    protected $name;

    protected $age;

    /**
     * User constructor.
     *
     * @param $name
     */
    public function __construct(string $name, int $age)
    {
        $this->name = $name;
        $this->age = $age;
    }

    /**
     * @return mixed
     */
    public function getName() : string
    {
        return $this->name;
    }

    public function getAge() : int
    {
        return $this->age;
    }
}

function findUserByAge(int $age, array $users) : array
{
    $result = [];
    foreach ($users as $user) {
        if ($user->getAge() == $age) {
            if ($user->getName() …
Run Code Online (Sandbox Code Playgroud)

php arrays oop type-hinting php-7

61
推荐指数
4
解决办法
5万
查看次数

带有phpmyadmin的PHP 7提供了大量的弃用通知

我安装了运行PHP7和phpmyadmin的Ubuntu 16.04 LTS.但是,我得到了很多弃用通知,例如:

Deprecation Notice in ./../php/php-gettext/streams.php#48  
Methods with the same name as their class will not be constructors in a future version of PHP; StringReader has a deprecated constructor

Backtrace  
./../php/php-gettext/gettext.inc#41: require()  
./libraries/select_lang.lib.php#477: require_once(./../php/php-gettext/gettext.inc)  
./libraries/common.inc.php#569: require(./libraries/select_lang.lib.php)  
./index.php#12: require_once(./libraries/common.inc.php)
Run Code Online (Sandbox Code Playgroud)

这是一个问题吗?如何摆脱这些通知(每次加载页面或执行操作时它们会弹出)?

phpmyadmin php-7 ubuntu-16.04

57
推荐指数
9
解决办法
5万
查看次数

如何在php 7中启用MySQLi扩展?

我在Ubuntu 14.04上安装了PHP 7,MySQL 5.5.47.我已经检查了已安装的扩展程序

sudo apt-cache search php7-*
Run Code Online (Sandbox Code Playgroud)

它输出我:

php7.0-common - Common files for packages built from the PHP source
libapache2-mod-php7.0 - server-side, HTML-embedded scripting language (Apache 2 module)
php7.0-cgi - server-side, HTML-embedded scripting language (CGI binary)
php7.0-cli - command-line interpreter for the PHP scripting language
php7.0-phpdbg - server-side, HTML-embedded scripting language (PHPDBG binary)
php7.0-fpm - server-side, HTML-embedded scripting language (FPM-CGI binary)
libphp7.0-embed - HTML-embedded scripting language (Embedded SAPI library)
php7.0-dev - Files for PHP7.0 module development
php7.0-dbg - Debug …
Run Code Online (Sandbox Code Playgroud)

php mysql mysqli php-7

53
推荐指数
6
解决办法
26万
查看次数

PHP 7 RC3:如何安装缺少的MySQL PDO

我正在尝试使用PHP 7 RC3+ Nginxon 设置webserver Ubuntu 14.04(用于测试目的).

我使用ubuntu/trusty64来自OndřejSurý的PHP 7 RC 3(https://launchpad.net/~ondrej/+archive/ubuntu/php-7.0)在Vagrant中安装了Ubuntu .

我找不到安装方式MySQL PDO(PHP看到PDO类,但没有任何与MySQL相关的东西,比如PDO::MYSQL_ATTR_DIRECT_QUERY等)

看起来没有LIB php7.0-mysql(通过与标准类比php5-mysqlndphp7.0-fpm从的Ondrej等)

PDOphpinfo():

PDO support      enabled
PDO drivers      no value
Run Code Online (Sandbox Code Playgroud)

我怎么才能得到它?

php mysql ubuntu pdo php-7

50
推荐指数
5
解决办法
19万
查看次数

PHP中的C#的空合并运算符(??)

在PHP中是否有像??C#一样的三元运算符?

?? 在C#中是干净和短,但在PHP中,你必须做的事情如下:

// This is absolutely okay except that $_REQUEST['test'] is kind of redundant.
echo isset($_REQUEST['test'])? $_REQUEST['test'] : 'hi';

// This is perfect! Shorter and cleaner, but only in this situation.
echo null? : 'replacement if empty';

// This line gives error when $_REQUEST['test'] is NOT set.
echo $_REQUEST['test']?: 'hi';
Run Code Online (Sandbox Code Playgroud)

php ternary-operator null-coalescing-operator php-7

49
推荐指数
4
解决办法
1万
查看次数

升级到php-7后"调用未定义的函数mysql_connect()"

在我将php5升级到php7之后,我收到错误500

PHP致命错误:未捕获错误:调用未定义函数mysql_connect()

我把它放到我的apt源码中以便立即获得php7:

deb http://packages.dotdeb.org jessie all
deb-src http://packages.dotdeb.org jessie all

我基本上做的是:

apt-get remove php5
apt-get install php7-*
Run Code Online (Sandbox Code Playgroud)

我正在使用当前版本的Debian Jessie.

但我仍然得到这个.这里有很多关于SO的问题我绝对检查过它们.但我还没有找到答案.

php debian php-7

48
推荐指数
1
解决办法
18万
查看次数

有没有办法在PHP中扩展特征?

我想使用现有的功能traittrait在其上创建我自己的功能,只是稍后将其应用于类.

确切地说,我想扩展Laravel SoftDeletestrait来制作SaveWithHistory函数,因此它会创建一个记录当前状态的副本作为已删除的记录.我也想用record_made_by_user_id字段来扩展它.

php traits laravel php-7

47
推荐指数
2
解决办法
1万
查看次数

无效为返回类型

我用PHP 7测试了返回类型.

我创建了一个简单的脚本来测试PHP 7的返回类型:

<?php

Class Obj {

    public function __construct(){

    }

    public function test(): string { //a string needs to be returned
        return "ok";
    }

}

function foo(): Obj { //instance of Obj needs to be returned
    return new Obj();
}

$o = foo();
echo $o->test(); // output: ok
Run Code Online (Sandbox Code Playgroud)

现在,当您指定返回类型时,在其他编程语言中,void这意味着您无法返回任何内容,否则您将收到错误.所以我写了这个脚本:

<?php

    function foo(): void {

    }

    foo(); 
Run Code Online (Sandbox Code Playgroud)

现在在上面的脚本中,预期的输出是什么.相反,它给了我一个致命的错误:

致命错误:foo()的返回值必须是void的实例,在第2行返回none

我的问题是(我找不到),在PHP 7中会有类似的void类型吗?

php return-type void php-7

42
推荐指数
3
解决办法
4万
查看次数