php中面向对象概念的问题

时间:2012-06-08 04:14:02

标签: php

我创建了一个名为classperson.php的文件,我在其中声明了所有变量。

<?php
class person1
{
    var $location;
    var $effective;
    var $training;
    var $treated;
    var $effort;
    var $information;
    var $recognition;
    var $valued;
    var $supervisor;
    var $informed;
    var $job;
    var $treats;
    var $satisfy;
    var $workgroup;
    var $utc;
    var $skills;

    function set_location($new_location)
    {
        $this->location=$new_location;
    }
    function get_location()
    {
        return $this->location;
    }


    function set_effective($new_effective)
    {
        $this->effective=$new_effective;
    }
    function get_effective()
    {
        return $this->effective;
    }



    function set_training($new_training)
    {
            $this->training=$new_training;
    }
    function get_training()
    {
            return $this->training;
    }



    function set_treated($new_treated)
    {
        $this->treated=$new_treated;
    }
    function get_treated()
    {
        return $this->treated;
    }




    function set_effort($new_effort)
    {
        $this->effort=$new_effort;
    }
    function get_effort()
    {
        return $this->effort;
    }



    function set_information($new_information)
    {
        $this->information=$new_information;
    }
    function get_information()
    {
            return $this->information;
    }



    function set_recognition($new_recognition)
    {
        $this->recognition=$new_recognition;
    }
    function get_recognition()
    {
        return $this->recognition;
    }



    function set_valued($new_valued)
    {
        $this->valued=$new_valued;
    }
    function get_valued()
    {
            return $this->valued;
    }



    function set_supervisor($new_supervisor)
    {
        $this->supervisor=$new_supervisor;
    }
    function get_supervisor()
    {
        return $this->supervisor;
    }



    function set_informed($new_informed)
    {
        $this->informed=$new_informed;
    }
    function get_informed()
    {
        return $this->informed;
    }



    function set_job($new_job)
    {
        $this->job=$new_job;
    }
    function get_job()
    {
            return $this->job;
    }



    function set_treats($new_treats)
    {
        $this->treats=$new_treats;
    }
    function get_treats()
    {
        return $this->treats;
    }



    function set_satisfy($new_satisfy)
    {
        $this->satisfy=$new_satisfy;
    }
    function get_satisfy()
    {
            return $this->satisfy;
    }



    function set_workgroup($new_workgroup)
    {
        $this->workgroup=$new_workgroup;
    }
    function get_workgroup()
    {
            return $this->workgroup;
    }



    function set_utc($new_utc)
    {
        $this->utc=$new_utc;
    }
    function get_utc()
    {
        return $this->utc;
    }



    function set_skills($new_skills)
    {
        $this->skills=$new_skills;
    }
    function get_skills()
    {
        return $this->skills;
    }
}
?>

后来我把这个文件包含在oopossurvey.php中。这是一个有5页的表格

查看代码

<?php
session_start();
$session=session_id();
$_SESSION['session']=$session;
?>
<html>
<body>
<form action="oopssurvey.php" method="post">
Where do you work?<br/>
<Input type ="radio" name="location" value="USA" <?php if($_SESSION['location'] == 'USA') echo "checked='checked'" ?>/>In the United states<br/>
<Input type ="radio" name="location" value="India" <?php if($_SESSION['location'] == 'India') echo "checked='checked'" ?>/>Outside of the United states<br/>

<input name="Operation1" type="submit" value="saveandresume" />
<input name="Operation2" type="submit" value="next" />

</form>
</body>
</html>

<?php
session_start();

if($_SESSION['session']==$session)
{
$location=$_SESSION['location'];
}
else
{
$_SESSION['location'] = $_POST['location'];
}
?>

<html>

<body>

<form action="oopssurvey.php" method="post">

In my work group, we are actively involved in making our work processes more effective and efficient (simpler, faster, etc.) using ACE.<br/>
<Input type ="radio" name="effective" value="Agree"/>Agree<br/>
<Input type ="radio" name="effective" value="Agreenordisagree"/>Neither Agree nor Disagree<br/>
<Input type ="radio" name="effective" value="disagree"/>Disagree<br/>

I have received the training I need to understand and implement ACE in my work group.<br/>
<Input type ="radio" name="training" value="Agree"/>Agree<br/>
<Input type ="radio" name="training" value="Agreenordisagree"/>Neither Agree nor Disagree<br/>
<Input type ="radio" name="training" value="disagree"/>Disagree<br/>

All employees at the company are treated fairly regardless of differences.<br/>
<Input type ="radio" name="treated" value="Agree"/>Agree<br/>
<Input type ="radio" name="treated" value="Agreenordisagree"/>Neither Agree nor Disagree<br/>
<Input type ="radio" name="treated" value="disagree"/>Disagree<br/>

Sufficient effort is made to get the opinions and thoughts of the people who work here.<br/>
<Input type ="radio" name="effort" value="Agree"/>Agree<br/>
<Input type ="radio" name="effort" value="Agreenordisagree"/>Neither Agree nor Disagree<br/>
<Input type ="radio" name="effort" value="disagree"/>Disagree<br/>

<input name="Operation3" type="submit" value="saveandresume" />
<input name="Operation4" type="submit" value="next" />
<input name="Operation5" type="submit" value="back" />

</form>

</body>

</html>


 <?php
session_start();

if($_SESSION['session']==$session)
{

    $effective=$_SESSION['effective'];

    $training=$_SESSION['training'];

    $treated=$_SESSION['treated'];

    $effort=$_SESSION['effort'];
}
else
{
    $_SESSION['effective']=$_POST['effective'];
    $_SESSION['training']=$_POST['training'];
    $_SESSION['treated']=$_POST['treated'];
    $_SESSION['effort']=$_POST['effort'];
}

?>

<html>
<body>
<form action="oopssurvey.php" method="post">

How satisfied are you with the information you receive from management on what's going on in the company?<br/>
<Input type ="radio" name="information" value="agree"/>Agree<br/>
<Input type ="radio" name="information" value="Agreenordisagree"/>Neither Agree nor Disagree<br/>
<Input type ="radio" name="information" value="disagree"/>Disagree<br/>

How satisfied are you with the recognition you receive for doing a good job?<br/>
<Input type ="radio" name="recognition" value="agree"/>Agree<br/>
<Input type ="radio" name="recognition" value="Agreenordisagree"/>Neither Agree nor Disagree<br/>
<Input type ="radio" name="recognition" value="disagree"/>Disagree<br/>

I feel valued as an employee of the company.<br/>
<Input type ="radio" name="valued" value="agree"/>Agree<br/>
<Input type ="radio" name="valued" value="Agreenordisagree"/>Neither Agree nor Disagree<br/>
<Input type ="radio" name="valued" value="disagree"/>Disagree<br/>

My immediate supervisor does a good job of coaching/guiding me.<br/>
<Input type ="radio" name="supervisor" value="agree"/>Agree<br/>
<Input type ="radio" name="supervisor" value="Agreenordisagree"/>Neither Agree nor Disagree<br/>
<Input type ="radio" name="supervisor" value="disagree"/>Disagree<br/>

<input name="Operation6" type="submit" value="saveandresume" />
<input name="Operation7" type="submit" value="next" />
<input name="Operation8" type="submit" value="back" />

</form>
</body>
</html>

<?php
session_start();


if($_SESSION['session']==$session)
{

$information=$_SESSION['information'];

$recognition=$_SESSION['recognition'];

$valued=$_SESSION['valued'];

$supervisor=$_SESSION['supervisor'];
}
else
{
    $_SESSION['information']=$_POST['information'];
    $_SESSION['recognition']=$_POST['recognition'];
    $_SESSION['valued']=$_POST['valued'];
    $_SESSION['supervisor']=$_POST['supervisor'];
}
?>
<html>

<body>

<form action="oopssurvey.php" method="post">

I feel well informed about what is expected in my job.<br/>
<Input type ="radio" name="informed" value="agree"/>Agree<br/>
<Input type ="radio" name="informed" value="Agreenordisagree"/>Neither Agree nor Disagree<br/>
<Input type ="radio" name="informed" value="disagree"/>Disagree<br/>

Overall, how good a job do you feel is being done by your immediate supervisor<br/>
<Input type ="radio" name="job" value="agree"/>Agree<br/>
<Input type ="radio" name="job" value="Agreenordisagree"/>Neither Agree nor Disagree<br/>
<Input type ="radio" name="job" value="disagree"/>Disagree<br/>

My immediate supervisor treats me with respect and dignity.<br/>
<Input type ="radio" name="treats" value="agree"/>Agree<br/>
<Input type ="radio" name="treats" value="Agreenordisagree"/>Neither Agree nor Disagree<br/>
<Input type ="radio" name="treats" value="disagree"/>Disagree<br/>

How satisfied are you with your involvement in the decisions that affect your work?<br/>
<Input type ="radio" name="satisfy" value="agree"/>Agree<br/>
<Input type ="radio" name="satisfy" value="Agreenordisagree"/>Neither Agree nor Disagree<br/>
<Input type ="radio" name="satisfy" value="disagree"/>Disagree<br/>

<input name="Operation9" type="submit" value="saveandresume" />
<input name="Operation10" type="submit" value="next" />
<input name="Operation11" type="submit" value="back" />

</form>

</body>

</html>

<?php

session_start();

if($_SESSION['session']==$session)
{

$informed=$_SESSION['informed'];

$job=$_SESSION['job'];

$treats=$_SESSION['treats'];

$satisfy=$_SESSION['satisfy'];
}
else
{
    $_SESSION['informed']=$_POST['informed'];
    $_SESSION['job']=$_POST['job'];
    $_SESSION['treats']=$_POST['treats'];
    $_SESSION['satisfy']=$_POST['satisfy'];
}
?>

<html>

<body>

<form action="oopssurvey.php" method="post">

Day-to-day decisions and activities in my work group demonstrate that quality is a top priority.
<Input type ="radio" name="workgroup" value="agree"/>Agree<br/>
<Input type ="radio" name="workgroup" value="Agreenordisagree"/>Neither Agree nor Disagree<br/>
<Input type ="radio" name="workgroup" value="disagree"/>Disagree<br/>

I have the opportunity to grow and develop at my company and/or elsewhere in UTC.
<Input type ="radio" name="utc" value="agree"/>Agree<br/>
<Input type ="radio" name="utc" value="Agreenordisagree"/>Neither Agree nor Disagree<br/>
<Input type ="radio" name="utc" value="disagree"/>Disagree<br/>

My job makes good use of my skills and abilities.
<Input type ="radio" name="skills" value="agree"/>Agree<br/>
<Input type ="radio" name="skills" value="Agreenordisagree"/>Neither Agree nor Disagree<br/>
<Input type ="radio" name="skills" value="disagree"/>Disagree<br/>

<input name="Operation12" type="submit" value="submit" />
<input name="Operation13" type="submit" value="back" />



</form>

</body>

</html>

<?php
session_start();
include 'classperson.php';

if($_POST['Operation1'])
{
$_SESSION['location'] = $location;
$_SESSION['location'] = $_POST['location'];
echo "Please save the link inorder to continue your survey http://bonnie/~jnagesh/SampleSurvey/OOPS/oopspage2.php";
}

else if($_POST['Operation2'])
{
$_SESSION['location'] = $location;
$_SESSION['location'] = $_POST['location'];
include "oopspage2.php";
}


elseif($_POST['Operation3'])

{
$_SESSION['effective'] = $effective;
$_SESSION['effective'] = $_POST['effective'];
$_SESSION['training'] = $training;
$_SESSION['training'] = $_POST['training'];
$_SESSION['treated'] = $treated;
$_SESSION['treated'] = $_POST['treated'];
$_SESSION['effort'] = $effort;
$_SESSION['effort'] = $_POST['effort'];
echo "Please save the link inorder to continue your survey http://bonnie/~jnagesh/SampleSurvey/OOPS/oopspage3.php";

}

elseif($_POST['Operation4'])
{
$_SESSION['effective'] = $effective;
$_SESSION['effective'] = $_POST['effective'];
$_SESSION['training'] = $training;
$_SESSION['training'] = $_POST['training'];
$_SESSION['treated'] = $treated;
$_SESSION['treated'] = $_POST['treated'];
$_SESSION['effort'] = $effort;
$_SESSION['effort'] = $_POST['effort'];
include "oopspage3.php";
}

elseif($_POST['Operation5'])
{
$_SESSION['location'] = $location;
$_SESSION['location'] = $_POST['location'];
include "oopspage1.php";
}

elseif($_POST['Operation6'])
{
$_SESSION['information'] = $information;
$_SESSION['information'] = $_POST['information'];
$_SESSION['recognition'] = $recognition;
$_SESSION['recognition'] = $_POST['recognition'];
$_SESSION['valued'] = $valued;
$_SESSION['valued'] = $_POST['valued'];
$_SESSION['supervisor'] = $supervisor;
$_SESSION['supervisor'] = $_POST['supervisor'];
echo "Please save the link inorder to continue your survey http://bonnie/~jnagesh/SampleSurvey/OOPS/oopspage4.php";

}

elseif($_POST['Operation7'] )
{
$_SESSION['information'] = $information;
$_SESSION['information'] = $_POST['information'];
$_SESSION['recognition'] = $recognition;
$_SESSION['recognition'] = $_POST['recognition'];
$_SESSION['valued'] = $valued;
$_SESSION['valued'] = $_POST['valued'];
$_SESSION['supervisor'] = $supervisor;
$_SESSION['supervisor'] = $_POST['supervisor'];
include "oopspage4.php";
}

elseif($_POST['Operation8'])
{
include "oopspage2.php";
}

elseif($_POST['Operation9'])
{
$_SESSION['informed'] = $informed;
$_SESSION['informed'] = $_POST['informed'];
$_SESSION['job'] = $job;
$_SESSION['job'] = $_POST['job'];
$_SESSION['treats'] = $treats;
$_SESSION['treats'] = $_POST['treats'];
$_SESSION['satisfy'] = $satisfy;
$_SESSION['satisfy'] = $_POST['satisfy'];
echo "Please save the link inorder to continue your survey http://bonnie/~jnagesh/SampleSurvey/OOPS/oopspage5.php";

}

elseif($_POST['Operation10'])
{
$_SESSION['informed'] = $informed;
$_SESSION['informed'] = $_POST['informed'];
$_SESSION['job'] = $job;
$_SESSION['job'] = $_POST['job'];
$_SESSION['treats'] = $treats;
$_SESSION['treats'] = $_POST['treats'];
$_SESSION['satisfy'] = $satisfy;
$_SESSION['satisfy'] = $_POST['satisfy'];
include "oopspage5.php";
}
elseif($_POST['Operation11'])
{
include "oopspage3.php";
}

elseif($_POST['Operation12'])
{
$con = mysql_connect("localhost","cgiadmin","cgi");
if (!$con)
 {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("j2", $con);

$person = new person1;
$person->set_location($_POST['location']);
$person->set_effective($_POST['effective']);
$person->set_training($_POST['training']);
$person->set_treated($_POST['treated']);
$person->set_effort($_POST['effort']);
$person->set_information($_POST['information']);
$person->set_recognition($_POST['recognition']);
$person->set_valued($_POST['valued']);
$person->set_supervisor($_POST['supervisor']);
$person->set_informed($_POST['informed']);
$person->set_job($_POST['job']);
$person->set_treats($_POST['treats']);
$person->set_satisfy($_POST['satisfy']);
$person->set_workgroup($_POST['workgroup']);
$person->set_utc($_POST['utc']);
$person->set_skills($_POST['skills']);


$sql="INSERT INTO bigsurvey(location, effective, training , treated , effort, information , recognition,valued,supervisor,informed,job,treats,satisfy,workgroup,utc,skills)
VALUES
('$_SESSION[location]','$_SESSION[effective]','$_SESSION[training]','$_SESSION[treated]','$_SESSION[effort]','$_SESSION[information]','$_SESSION[recognition]','$_SESSION[valued]','$_SESSION[supervisor]','$_SESSION[informed]','$_SESSION[job]','$_SESSION[treats]','$_SESSION[satisfy]','".$person->get_workgroup()."','".$person->get_utc()."','".$person->get_skills()."')";
if (!mysql_query($sql,$con))
  {
    die('Error: ' . mysql_error());
  }
echo "Thanks for taking the survey .";
mysql_close($con);

}
elseif($_POST['Operation13'])
{
include "oopspage4.php";
}
?>

我的问题是,这是在php中使用面向对象的概念来创建或创建表单的正确方法吗?或者我应该创建更多对象还是进行任何更改?

我试图在所有页面上包含文件classperson.php,但是当我使用下一个按钮时出现错误。

提前致谢

1 个答案:

答案 0 :(得分:3)

首先,您所谓的“页面”仅用于客户端,但对于PHP,它只是一个脚本。所以没有任何意义,例如多次执行session_start(),因为每次将数据发送到服务器时,脚本都会从头开始再次运行。当它到达当前页面的末尾时也不会停止运行。

其次,这个通过会话变量确定调查步骤的想法本身就有点生锈,而且没有正确实施。我很确定它不起作用(虽然我没有运行代码)。在会话中存储变量没什么不好,但我的建议是你应该以某种方式重新思考逻辑。例如,您可以在每个表单中使用一些隐藏的输入来检查用户的调查步骤,并在PHP文件中为每个页面添加一些switch{}()个案例。

第三,不要太困惑,你应该分开逻辑和表达。我的意思是 - 为所有表单使用单独的文件,并使用单独的文件接受来自用户的数据,将其写下来(如在您的示例中 - 在会话中),并选择接下来要显示的页面。通过这种方式,您可以更轻松地本地化错误,并且可以更好地解决问题。

至于你在PHP中使用类的问题:通常你应该只包括一次类,不需要重新包含它,例如在此文件中,您将处理所有逻辑。然后,您可以创建该类的实例并使用它。无论如何,我有一种感觉,你有点匆忙,在我看来,你早期开始使用OOP技术 - 最好一步一步走。尝试使这个脚本在没有类的情况下工作。什么时候它会将逻辑提取到一个类。那样你就能更好地理解一切:)