面向对象的php类简单的例子

Aff*_*mad 12 php oop

我是PHP的初学者,所以现在我尝试学习面向对象我是g目一点,我得到了一些想法,但不清楚的概念.所以我来到那里.请任何PHP大师给出简单的例子,如何crate类和如何调用其他php页.

例如

我想要两个类show name,第二个是enter name.第一类显示名称此名称来自数据库,第二类将名称放在数据库中.

的index.php

<form action="checking.php" method="post">
      <input type="text" placeholder="Please enter name">
</form>
Run Code Online (Sandbox Code Playgroud)

Vee*_*tav 14

你调用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.
        **/
    }
}
?>
Run Code Online (Sandbox Code Playgroud)

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. 
?>
Run Code Online (Sandbox Code Playgroud)

这样你就可以实现这一点,这只是一个概述.它远不止于此.