PHP:如何使用类函数?

时间:2015-06-20 07:32:12

标签: php class object

我很新,并且正在进行一些试验和测试,创建一个类和对象。

这是我的剧本:

<?php
class testingm 
{
private $t;        
public function __construct($file) 
{
$this->t = fopen($file, 'rb')
}      
}

$test = new testingm;
$s = new file($_GET['n']);
?>

脚本说

警告:缺少参数1 for testingm :: __ construct(),在

中调用

我如何为$ file变量提供值?有人可以指导我吗?

2 个答案:

答案 0 :(得分:2)

您的constructor method __construct($file)需要1个参数$file,需要提供该参数。使用带有new的类实例化对象时,将调用构造函数方法。

要在您的示例中执行此操作,请在使用new实例化对象时传入文件名。例如:

$file = $_GET['n'];
$test = new testingm($file);

PHP文档提供了有关function arguments的更多信息,用于将参数传递给方法。

答案 1 :(得分:1)

$test = new testingm($_GET['n']);