如何创建和访问PHP对象变量

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (300)

如果你从“Medoo”看下面的内容:

// If you installed via composer, just use this code to requrie autoloader on the top of your projects.
require 'vendor/autoload.php';

// Using Medoo namespace
use Medoo\Medoo;

// Initialize
$database = new Medoo([
    'database_type' => 'mysql',
    'database_name' => 'name',
    'server' => 'localhost',
    'username' => 'your_username',
    'password' => 'your_password'
]);

// Enjoy
$database->insert('account', [
    'user_name' => 'foo',
    'email' => 'foo@bar.com'
]);

$data = $database->select('account', [
    'user_name',
    'email'
], [
    'user_id' => 50
]);

echo json_encode($data);

// [
//     {
//         "user_name" : "foo",
//         "email" : "foo@bar.com"
//     }
// ]        

我试图弄清楚如何在类中创建函数,可以通过这种方式定义和访问。所以我的第一个问题是如何使用以下格式在类中编写函数:

$database->insert('account', [
    'user_name' => 'foo',
    'email' => 'foo@bar.com'
]);

然后我将如何使用“echo”访问它或再次更改它

我创造了一些设计/测试的东西。如果你能给我一个重做的例子我会非常感激。

class Test_Class {

    /*  
        // Want to set up and access like:
        $Test_Run -> Test_Function_1([
        'TF1_Var_1' => 'TF1_Val_1',
        'TF1_Var_2' => 'TF1_Val_2',
        'TF1_Var_3' => 'TF1_Val_3',
    ]);
    */
    public function Test_Function_1 (){

    } // END function Test_Function_1

    /*  
        // Want to set up and access like:
    $Test_Run -> Test_Function_2 ('TF2_Selector', [
        'TF1_Var_1' => 'TF1_Val_1',
        'TF1_Var_2' => 'TF1_Val_2',
        'TF1_Var_3' => 'TF1_Val_3'
    ]);
    */
    public function Test_Function_2 (){

    } // END function Test_Function_2

} // END class Test_Class 

$Test_Run = new Test_Class;

// Not sure how to set or change ???      
$Test_Run -> Test_Function_1([
    'TF1_Var_1' => 'TF1_Val_1',
    'TF1_Var_2' => 'TF1_Val_2',
    'TF1_Var_3' => 'TF1_Val_3',
]);   

// Not sure how to set or change ???          
$Test_Run -> Test_Function_2 ('TF2_Selector', [
    'TF2_Var_1' => 'TF2_Val_1',
    'TF2_Var_2' => 'TF2_Val_2',
    'TF2_Var_3' => 'TF2_Val_3'
]);

// Not Sure how to access/echo ???    
echo $Test_Run -> Test_Function_1 ( 'TF1_Var_1' ); // ???
echo $Test_Run -> Test_Function_1 [ 'TF1_Var_1' ]; // ???
echo $Test_Run -> Test_Function_1 ([ 'TF1_Var_1' ]); // ???

// Not Sure how to access/echo ???    
echo $Test_Run -> Test_Function_2 ( 'TF2_Selector'  ); // ???
echo $Test_Run -> Test_Function_2 [ 'TF2_Selector' ]; // ???
echo $Test_Run -> Test_Function_2 ([ 'TF2_Selector' ]); // ???

echo $Test_Run -> Test_Function_2 ( 'TF2_Var_1' ); // ???
echo $Test_Run -> Test_Function_2 [ 'TF2_Var_1' ]; // ???
echo $Test_Run -> Test_Function_2 ([ 'TF2_Var_1' ]); // ???
提问于
用户回答回答于

您应该阅读本手册

class TestClass { 
    public function test1(array $array) {
        var_dump($array);
    }
    public function test2($selector, array $array) {
        var_dump($selector, $array);
    }
}

$test = new TestClass();
$test->test1([ 'TF1_Var_1' => 'TF1_Val_1', 'TF1_Var_2' => 'TF1_Val_2', 'TF1_Var_3' => 'TF1_Val_3' ]);
$test->test2("TF2_Selector", [ 'TF1_Var_1' => 'TF1_Val_1', 'TF1_Var_2' => 'TF1_Val_2', 'TF1_Var_3' => 'TF1_Val_3' ]);

扫码关注云+社区

领取腾讯云代金券

玩转腾讯云 有奖征文活动