关于在类中调用PHP函数的一些说明
我知道这是一个基本的php问题,但我有一些不明白的地方,那就是关于在类中调用PHP函数的一些说明,php,class,zend-framework,Php,Class,Zend Framework,我知道这是一个基本的php问题,但我有一些不明白的地方,那就是返回\u checkthis($name)而不是$this->\u checkThat($name)将正常工作并停止下一个代码的执行,这是逻辑正确的吗?返回中的函数结束函数执行。谢谢,调用函数时的返回很好返回$this->\u checkthis($name)和非$this->\u检查那个($name)
返回当我调用一个函数时,下面的代码只是一个例子
案例:在TEST2中
当我进行检查时,我返回,返回代码>执行作业并停止执行下一个代码TEST3
Good
现在在TEST1
中,我调用了一个函数\u checkThat()
,这个函数执行一个检查,然后重定向。问题是它返回舒尔,但下一个代码也将执行TEST2
和TEST3
为什么?为什么当我将该函数的内容直接放入TEST1
中时,它会返回并停止执行下一个代码
<?php class Some_Class_IndexController extends Some_Other_Controller_Front_Action
{
$name = $this->getRequest()->getParam('name'); //Output: "john"
$email = $this->getRequest()->getParam('email'); //Output: "john@gmail.com"
$phone = $this->getRequest()->getParam('phone'); //Output: 09000000
//TEST1
if(isset($name)) {
$this->_checkThat($name);
}
//TEST2
if(isset($email)) {
if($email === "john@gmail.com") {
//some code to redirect to another page
return;
} else {
//some code to redirect to another page
return;
}
}
//TEST3
if(isset($name)) {
$this->_checkthat();
}
private function _checkThat($name) {
if ($name !== "john") {
//some code to redirect to another page
return;
}
}
}
尽管如注释中所述,您的示例代码不正确,但您的问题如下:
if(isset($name)) {
$this->_checkThat($name);
}
是您对\u checkthit()
返回的结果不执行任何操作。显然,您应该返回它:
if(isset($name)) {
return $this->_checkThat($name);
}
作为旁注,我应该提到的是,自php5以来,使用\uu
来标记受保护的/私有的命名方法已经过时了。下面是一个简单的指南
继承–以面向对象的方式重用代码
继承是面向对象编程中的一个基本功能/构造,您可以使用一个类作为另一个类或许多其他类的基/基础
为什么这样做?
这样做可以有效地重用基类中的代码
比如说,您想创建一个新的employee类
,因为我们可以说employee
是一种类型/种类的“person”,它们将共享公共属性和方法
有意义吗?
在这种情况下,继承可以使代码更轻,因为您在两个不同的类中重用相同的代码。但与旧式PHP不同的是:
您只需键入一次代码。
被重用的实际代码可以在许多(无限)类中重用
但从概念上讲,它只在一个地方打印出来,这是一种
像PHP includes()
请看一看示例PHP代码:
// 'extends' is the keyword that enables inheritance
class employee extends person
{
function __construct($employee_name) {
$this->set_name($employee_name);
}
}
通过继承重用代码:
由于类employee
基于类person
,employee
自动具有“person”的所有公共和受保护属性和方法
书呆子注:书呆子会说员工是一种人
守则:
class employee extends person
{
function __construct($employee_name){
$this->set_name($employee_name);
}
}
<?phpinclude("class_lib.php"); ?>
<?php
// Using our PHP objects in our PHP pages.
$stefan = new person("Stefan Mischook");
echo "Stefan's full name: " . $stefan->get_name();
$james = new employee("Johnny Fingers");
echo "---> " . $james->get_name();
?>
<?php
class person
{
// explicitly adding class properties are optional - but
// is good practice
var $name;
function __construct($persons_name) {
$this->name = $persons_name;
}
public function get_name() {
return $this->name;
}
// protected methods and properties restrict access to
// those elements.
protected function set_name($new_name) {
if ($this->name != "Jimmy Two Guns") {
$this->name = strtoupper($new_name);
}
}
}
// 'extends' is the keyword that enables inheritance
class employee extends person
{
protected function set_name($new_name) {
if ($new_name == "Stefan Sucks") {
$this->name = $new_name;
}
else if ($new_name == "Johnny Fingers") {
person::set_name($new_name);
}
}
function __construct($employee_name)
{
$this->set_name($employee_name);
}
}
?>
protected function set_name($new_name)
{
if ($new_name == "Stefan Sucks") {
$this->name = $new_name;
}
else if ($new_name == "Johnny Fingers") {
parent::set_name($new_name);
}
}
请注意,我们如何能够在employee
中使用set_name(),即使我们没有在employee
类中声明该方法。这是因为我们已经在类person
中创建了set_name()
书呆子注:person
类被书呆子称为base
类或parent
类,因为它是员工
所基于的类。当您的项目变得更加复杂时,这个类层次结构可能会变得非常重要
通过继承重用代码:
正如您在下面的代码片段中所看到的,我们可以在employee
对象上调用get_name,这是person
提供的
守则:
class employee extends person
{
function __construct($employee_name){
$this->set_name($employee_name);
}
}
<?phpinclude("class_lib.php"); ?>
<?php
// Using our PHP objects in our PHP pages.
$stefan = new person("Stefan Mischook");
echo "Stefan's full name: " . $stefan->get_name();
$james = new employee("Johnny Fingers");
echo "---> " . $james->get_name();
?>
<?php
class person
{
// explicitly adding class properties are optional - but
// is good practice
var $name;
function __construct($persons_name) {
$this->name = $persons_name;
}
public function get_name() {
return $this->name;
}
// protected methods and properties restrict access to
// those elements.
protected function set_name($new_name) {
if ($this->name != "Jimmy Two Guns") {
$this->name = strtoupper($new_name);
}
}
}
// 'extends' is the keyword that enables inheritance
class employee extends person
{
protected function set_name($new_name) {
if ($new_name == "Stefan Sucks") {
$this->name = $new_name;
}
else if ($new_name == "Johnny Fingers") {
person::set_name($new_name);
}
}
function __construct($employee_name)
{
$this->set_name($employee_name);
}
}
?>
protected function set_name($new_name)
{
if ($new_name == "Stefan Sucks") {
$this->name = $new_name;
}
else if ($new_name == "Johnny Fingers") {
parent::set_name($new_name);
}
}
请注意,employee
类中的set_name()与父类:person
中的版本有何不同
您的Some_类
不是有效的PHP类。它有很多方法之外的代码,这是你无法拥有的。请给我们一个适当的例子。是的,我知道,但这不是问题,而是内部逻辑很重要,我的类工作得很好。发布的类工作得不好。它会抛出语法错误。如果要在类中使用逻辑,则逻辑必须位于方法内部。当你向我们寻求帮助时,你需要给我们一个合适且可测试的例子。我告诉过你,我的类运行良好,这只是一个例子,无论如何,谢谢你的时间。不工作的例子是一个坏例子。好的,所以返回$this->\u checkthis($name)
而不是$this->\u checkThat($name)
将正常工作并停止下一个代码的执行,这是逻辑正确的吗?返回
中的函数结束函数执行。谢谢,调用函数时的返回很好返回$this->\u checkthis($name)代码>和非$this->\u检查那个($name)代码>