我的代码有问题.我在php页面中包含了两次相同的类,当其中一个工作时,其他的不起作用.我的代码可能比我更好解释.
<?php
if($_POST['submit']){
if(!class_exists('Some_Class'){
include('Class.php');
$obj = new Some_Class;
$obj->Do_something_2();
}
unset($obj);
}
?>
<html>
<head></head>
<body>
<form method="post" action="">
<?php
if(!class_exists('Some_Class')){
include('Class.php');
$obj = new Some_Class;
$obj->Do_something_1();
}
unset($obj);
?>
<input type="text" name=""/>
<input type="submit" name="submit"/>
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
现在发生的实际问题是,我在程序上在表单中添加了一些html表单元素.当我提交表单时,我有一点检查完成回发,如果回发设置,那么我再次包括相同的类,但在类中执行其他功能.所以当我第一次刷新页面时,第一堂课包含在表单中工作正常,但在提交表单时,类包含在检查后期工作中,但不包括表单中的表.希望我能够解决我的问题,请告诉我这种编写代码的方法有什么问题.
那是因为class_exists('Some_Class'))第二次是真的.你必须搬家
$obj = new Some_Class;
$obj->Do_something_1();
Run Code Online (Sandbox Code Playgroud)
在那条件之外.
更好的解决方案是include_once在页面顶部的类.
更好的解决方案是自动加载它.
请注意,即使您正在使用类,这仍然是一种程序样式.