包含和复制函数定义的问题 [英] Problems with include and duplication function definitions

查看:62
本文介绍了包含和复制函数定义的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它使用几种文件格式来存储类似的数据。

所以我已经为包含相同的

函数的每种格式创建了各种php文件相同的最终结果。


现在我有一个只读取文件的函数设置和

确定它是哪种文件类型并包含正确的源。我的
目标是为每个源添加一个新函数,名为

My_File_Format($ filename)其中每个文件检查

指定的文件是它知道如何使用的格式。


我遇到的问题是重复的函数名称。这是一个

测试案例,说明我究竟想做什么。任何人都知道如何实际让这个工作吗?

输出:

测试1

致命错误:无法重新声明test_case()(先前在

C:\ Inetpub \Accounting\test1.php:5中声明)在C:\ Inetpub \Accounting\test2.php
$第6行b $ b


假设应该是

Test1Test2


file testcase.php

< html>

< body>

<?php


class file_format

{

var $ f;

函数file_format($ filename)

{

$ this - > f = $ filename;

}

函数is_format()

{

包括" $ this-> f";

test_case();

}

}


$ a = new file_format(" test1.php");

$ b = new file_format(" test2.php");

$ a-> is_format( );

$ b-> is_format();


返回;

?>

< / body>

< / html>


档案:test1.php

<?php


function test_case()

{

echo" Test1";

}

?>


档案:test2.php

<?php


function test_case()

{

echo" Test2";

}

?> ;

I have an application that uses several file formats for similar data.
So I''ve created various php files for each format containing the same
functions which produce the same end result.

Now I currently have a functions setup that just reads a file and
determines which file type it is and includes the correct source. My
goal is add a new function to each source called
My_File_Format($filename) where each file checks to see if the
specified file is the format it knows how to work with.

The problem I''m running into is duplicate function names. Here is a
test case of what exactly I was attempting to do. Anyone know how to
actually get this to work?
Output:
Test1
Fatal error: Cannot redeclare test_case() (previously declared in
C:\Inetpub\Accounting\test1.php:5) in C:\Inetpub\Accounting\test2.php
on line 6

Assumed should be
Test1Test2

file testcase.php
<html>
<body>
<?php

class file_format
{
var $f;
function file_format($filename)
{
$this->f = $filename;
}
function is_format()
{
include "$this->f";
test_case();
}
}

$a = new file_format("test1.php");
$b = new file_format("test2.php");
$a->is_format();
$b->is_format();

return;
?>
</body>
</html>

file: test1.php
<?php

function test_case()
{
echo "Test1";
}
?>

file: test2.php
<?php

function test_case()
{
echo "Test2";
}
?>

推荐答案

filename)其中每个文件检查

指定的文件是否为格式知道如何使用。


我遇到的问题是重复的函数名称。这是一个

测试案例,说明我究竟想做什么。任何人都知道如何实际让这个工作吗?

输出:

测试1

致命错误:无法重新声明test_case()(先前在

C:\ Inetpub \Accounting\test1.php:5中声明)在C:\ Inetpub \Accounting\test2.php
$第6行b $ b


假设应该是

Test1Test2


file testcase.php

< html>

< body>

<?php


class file_format

{

var
filename) where each file checks to see if the
specified file is the format it knows how to work with.

The problem I''m running into is duplicate function names. Here is a
test case of what exactly I was attempting to do. Anyone know how to
actually get this to work?
Output:
Test1
Fatal error: Cannot redeclare test_case() (previously declared in
C:\Inetpub\Accounting\test1.php:5) in C:\Inetpub\Accounting\test2.php
on line 6

Assumed should be
Test1Test2

file testcase.php
<html>
<body>
<?php

class file_format
{
var


f;

function file_format(
f;
function file_format(


filename )

{
filename)
{


这篇关于包含和复制函数定义的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆