面向对象的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.
?>
通过这种方式可以实现这一点,这只是一个概述.不仅如此.
i am beginner on php so now i try to learn object oriented i was goggling i got it some ideas but not clear concept.So i come there.Please any php guru give simple example of how to crate classes and how to call on other php page.
for example
i want two classes one is show name
and second one is enter name
.First class show name this name come from database and second class put name in database.
Index.php
<form action="checking.php" method="post">
<input type="text" placeholder="Please enter name">
</form>
The way you are calling a php page is good. That is from HTML.
What I think, you are getting this wrong. A class showName
to get name from database and enterName
to save in database. Well what I suggest that should be a function within one single class.
<?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.
**/
}
}
?>
In checking.php
you can include:
<?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.
?>
This way you can achieve this, this is just an overview. It is much more than that.
这篇关于面向对象的php类简单示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!