Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 代码点火器传递参数至功能_Php_Codeigniter - Fatal编程技术网

Php 代码点火器传递参数至功能

Php 代码点火器传递参数至功能,php,codeigniter,Php,Codeigniter,我是OOP新手,在理解OOP背后的结构方面遇到了一些困难。 我在Codeigniter(Template)中创建了一个库,在加载它时我会传递一些参数,但我想将这些参数传递给库的函数 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Template { public function __construct($params) { echo '&

我是OOP新手,在理解OOP背后的结构方面遇到了一些困难。 我在Codeigniter(Template)中创建了一个库,在加载它时我会传递一些参数,但我想将这些参数传递给库的函数

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Template {

    public function __construct($params)
    {
        echo '<pre>'; print_r($params); echo '</pre>';
        //these are the parameters I need. I've printed them and everything seems fine
    }

    public function some_function()
    {
        //I need the above parameters here
    }

}

像这样创建类成员:

class Template {

    // Set some defaults here if you want
    public $config = array(
        'item1'  =>  'default_value_1',
        'item2'  =>  'default_value_2',
    );
    // Or don't
    // public $config = array();

    // Set a NULL default value in case we want to use defaults
    public function __construct($params = NULL)
    {
        // Loop through params and override defaults
        if ($params)
        {
            foreach ($params as $key => $value)
            {
                $this->config[$key] = $value;
            }
        }
    }

    public function some_function()
    {
        //i need the above parameters here

        // Here you go
        echo $this->config['item1'];
    }

}

然后您可以在函数中使用它们

您可能希望将参数作为属性存储在类中,以便您的所有方法都可以访问它们

请参阅以下关于PHP 5中属性的文档:

编辑:事实上,如果你是OOP的新手,你会发现一开始很难理解。当你遇到问题时,一次只问一个问题是一种非常低效的方式。如果您想节省一些时间,我建议您从阅读一篇基本的文本开始,该文本将OOP的概念与特定语言的实现细节(例如)分开来解释。然后,当您需要详细信息时,您会发现它们非常好(而且是免费的)。

请尝试以下方法:

这将打开数组('item1'=>'value1','item2'=>'value2')
转换成您可以使用的东西,如
$this->config['item1']
。您只是将数组分配给类变量
$config
。您还可以循环遍历变量,并根据需要验证或更改它们

如果不想覆盖设置的默认值,只需不在
$params
数组中设置该项即可。根据需要使用尽可能多的不同变量和值,这取决于您:)


同时,一定要仔细阅读并尝试自己。这些文档可能会让人困惑,因为它们提供了很多边缘案例示例,但是如果您查看Codeigniter中的库,您可以看到一些示例或类属性的使用方式。这是你必须熟悉的基本知识。我建议你决定这个类的变量是私有的还是公共的。这对可读性有很大帮助。私有变量应用于内部变量,而公共变量应用于对象的属性。

您需要访问
$param1
$param2
作为
$this->param1
$this->param2
是的Bassney,一定要阅读php.net上的文档,然后进行实验,这是学习的好方法。查看Codeigniter中的其他一些类,以查看一些示例。非常感谢你。
class Template {

    // Set some defaults here if you want
    public $config = array(
        'item1'  =>  'default_value_1',
        'item2'  =>  'default_value_2',
    );
    // Or don't
    // public $config = array();

    // Set a NULL default value in case we want to use defaults
    public function __construct($params = NULL)
    {
        // Loop through params and override defaults
        if ($params)
        {
            foreach ($params as $key => $value)
            {
                $this->config[$key] = $value;
            }
        }
    }

    public function some_function()
    {
        //i need the above parameters here

        // Here you go
        echo $this->config['item1'];
    }

}