面向对象php类的简单示例


Object oriented php class simple example

我是php的初学者,所以现在我试着学习面向对象的,我当时很兴奋,我有一些想法,但概念不清楚。所以我来了。请任何php大师给出如何装箱类以及如何调用其他php页面的简单示例。

例如

我想要两个类,一个是show name,第二个是enter name。第一个类显示名称这个名称来自数据库,第二类将名称放入数据库。

Index.php

<form action="checking.php" method="post">
      <input type="text" placeholder="Please enter name">
</form>

调用php页面的方式很好。这是来自HTML。

我想,你错了。类showName从数据库中获取名称,类enterName保存在数据库中。我建议它应该是一个类中的函数。

<?php
class Name
{
    public $name;
    public function showName()
    {
        /**
        Put your database code here to extract from database.
        **/
        return($this->name);
    }
    public function enterName($TName)
    {
        $this->name = $TName;
        /**
        Put your database code here.
        **/
    }
}
?>

checking.php中,您可以包括:

<?php
    include_once("name_class.php");
    $name = $_POST['name'];   //add name attribute to input tag in HTML
    $myName = new Name();
    $myName->enterName($name); //to save in database/
    $name=$myName->showName(); //to retrieve from database. 
?>

通过这种方式你可以实现这一点,这只是一个概述。它远不止于此。

您必须创建一个类人员和两个方法。。

class Person{
    public $name;
        public function showName()
        {
             echo $this->name;
        }
        public function enterName()
        {
             //insert name into database
        }
}