【问题标题】:How to elinminate creating too many objects in this code php如何消除在此代码 php 中创建太多对象
【发布时间】:2017-07-03 10:31:38
【问题描述】:

这是我创建的用于调用我的数据库连接并从数据库中插入和检索数据的函数
有没有办法在不为每个函数创建变量的情况下做到这一点

//require('OrientDB/OrientDB.php');
require 'phpIncludes/PhpOrient/vendor/autoload.php';
use PhpOrient\PhpOrient;
//require 'phpIncludes/sendgrid-php/vendor/autoload.php';

//Used for insert and other non value returning operations just send the query to execute

function runInsertQuery($query) {

    $client = new PhpOrient();
    $client->hostname = 'localhost';
    $client->port = 2424;
    $client->username = 'root';
    $client->password = 'hello';
    $client->connect();
    $client->dbOpen('tabe');


    $result = $client->command($query);
   // var_dump($result);


    return $result;
}

function getId($table, $field, $value) {

    $client = new PhpOrient();
    $client->hostname = 'localhost';
    $client->port = 2424;
    $client->username = 'root';
    $client->password = 'hello';
    $client->connect();
    $client->dbOpen('tabe');

    $query = "select from $table where $field='$value'";
    $result = $client->command($query);

    $json = json_decode(json_encode($result));

    //var_dump($json);

    if (sizeof($json) > 0) {
        return $json->rid;
    } else {
        return false;
    }
}

function runSelectQuery($query) {

    $client = new PhpOrient();
    $client->hostname = 'localhost';
    $client->port = 2424;
    $client->username = 'root';
    $client->password = 'hello';
    $client->connect();
    $client->dbOpen('tabe');


    $result = $client->query($query);



    $json = json_decode(json_encode($result));



    if (sizeof($json) > 0) {
        return $json[0]->oData;
    } else {
        return false;
    }

}
function runquery($query)
{
    $client = new PhpOrient();
    $client->hostname = 'localhost';
    $client->port = 2424;
    $client->username = 'root';
    $client->password = 'hello';
    $client->connect();
    $client->dbOpen('tabe');


    $result = $client->query($query);



    $json = json_decode(json_encode($result));



    if (sizeof($json) > 0) {
        return $json;
    } else {
        return false;
    }

}

如果我这样做

    $client = new PhpOrient();
    $client->hostname = 'localhost';
    $client->port = 2424;
    $client->username = 'root';
    $client->password = 'hello';
    $client->connect();
    $client->dbOpen('tabe');
    function insertQuery();
     {
       //body
     } 
   function selectQuery();
     {
       //body
     } 

它抛出了变量未定义的错误如何解决这个问题 我是php新手,你能帮我解决这个问题吗

【问题讨论】:

  • 您应该将PhpOrient 继承到一个新类,然后使用新的类对象或将这些函数添加到PhpPrient 类中。
  • @HamzaAbdaoui Agghhhh 不要使用 GLOBAL 作为传递应该在函数参数中的数据的方式。全球是不懂的人的首选
  • @RiggsFolly Thnx 给小费!

标签: php variables scope


【解决方案1】:

一种可能的解决方案是将您的 $client 初始化代码包装在一个函数中,然后在其他函数中调用该函数,如下所示:

require 'phpIncludes/PhpOrient/vendor/autoload.php';

use PhpOrient\PhpOrient;

/**
 * 
 * @staticvar PhpOrient $client
 * @return PhpOrient
 */
function getClient()
{
    static $client;

    if (!($client instanceof PhpOrient)) {
        $client = new PhpOrient();
        $client->hostname = 'localhost';
        $client->port = 2424;
        $client->username = 'root';
        $client->password = 'hello';
        $client->connect();
        $client->dbOpen('tabe');
    }

    return $client;
}

function runInsertQuery($query)
{
    $result = getClient()->command($query);
    // var_dump($result);

    return $result;
}

function getId($table, $field, $value)
{
    $query = "select from $table where $field='$value'";
    $result = getClient()->command($query);

    $json = json_decode(json_encode($result));

    //var_dump($json);

    if (sizeof($json) > 0) {
        return $json->rid;
    } else {
        return false;
    }
}

function runSelectQuery($query)
{
    $result = getClient()->query($query);
    $json = json_decode(json_encode($result));

    if (sizeof($json) > 0) {
        return $json[0]->oData;
    } else {
        return false;
    }
}

function runquery($query)
{
    $result = getClient()->query($query);
    $json = json_decode(json_encode($result));

    if (sizeof($json) > 0) {
        return $json;
    } else {
        return false;
    }
}

【讨论】:

  • 这太好了,我一直在寻找这样的东西谢谢
猜你喜欢
  • 2019-08-22
  • 2011-04-02
  • 2022-01-26
  • 1970-01-01
  • 2021-10-13
  • 2015-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多