关于在类中调用PHP函数的一些解释

问题描述 投票:2回答:2

我知道这是基本的php问题,但我有一些我不理解的东西,它是关于return;当我调用一个函数时,波纹管代码只是一个例子。

案例:在TEST2,当我做我的检查,然后我回来,return;做的工作,并停止执行下一个代码TEST3好。

现在在TEST1我调用函数_checkThat(),并且此函数执行检查然后重定向。问题是它返回舒尔,但下一个代码也将执行TEST2TEST3为什么?为什么当我将该函数的内容直接放在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: "[email protected]" 
    $phone = $this->getRequest()->getParam('phone'); //Output: 09000000       

    //TEST1
    if(isset($name)) {
        $this->_checkThat($name);
    }

    //TEST2
    if(isset($email)) {
        if($email === "[email protected]") {
            //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;
        }
    }
}

其他问题,在这种情况下我可以使用continue;

if(isset($name)) {
    Mage::log('check passed'); //This just write in logs
    continue; // or something else
} else if (!isset($name)) {
    // some code
}
php class zend-framework
2个回答
1
投票

虽然正如评论中已经提到的,您的示例代码不正确,但您的问题在于:

if(isset($name)) {
    $this->_checkThat($name);
}

是你对_checkThat()返回的结果什么都不做。显然你应该return它:

if(isset($name)) {
    return $this->_checkThat($name);
}

作为旁注,我应该提到用_命名方法来标记它们protected / private是自php5以来的过时实践。


0
投票

这是一个简单的指南。

继承 - 以OOP方式重用代码

继承是面向对象编程中的基本功能/构造,您可以使用一个类作为另一个类或许多其他类的基础/基础。

为什么这样?

这样做可以有效地重用基类中的代码。

说,你想创建一个新的employee class,因为我们可以说employee是一种类型/类型的“人”,他们将分享共同的属性和方法。

有道理吗?

在这种情况下,继承可以使代码更轻,因为您在两个不同的类中重用相同的代码。但不像old-school 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自动拥有“人”的所有公共和受保护的属性和方法。

书呆子说:书呆子会说employee是一种人。

代码:

class employee extends person 
{
    function __construct($employee_name){
        $this->set_name($employee_name);
    }
}

注意我们如何在employee中使用set_name(),即使我们没有在employee类中声明该方法。那是因为我们已经在类person中创建了set_name()。

书呆子注意:person类被称为(通过书呆子)base类或parent类,因为它是employee所基于的类。当您的项目变得更加复杂时,此类层次结构可能会变得非常重要。

重用代码继承:

正如您在下面的代码片段中看到的,我们可以在employee对象上调用get_name,由person提供。

代码:

<?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();
    ?>

这是OOP如何减少代码行数(不必编写两次相同的方法)的经典示例,同时仍然保持代码模块化并且更易于维护。

压倒一切的方法1

有时(使用继承时),您可能需要更改方法在基类中的工作方式。

例如,让我们说'employee'类中的set_name()方法,必须做一些与person类不同的方法。

你通过在override中声明相同的方法来personemployee类版本的set_name()。

代码段:

<?php
    class person 
    {
        protected function set_name($new_name) {
            if ($new_name != "Jimmy Two Guns") {
                $this->name = strtoupper($new_name);
            }
        }
    } 

    class employee extends person 
    {
        protected function set_name($new_name) {
            if ($new_name == "Stefan Sucks") {
                $this->name = $new_name;
            }
        }
    }
?>

请注意employee类中的set_name()与父类中找到的版本不同:person

<覆盖方法:2

有时您可能需要访问您在派生(有时称为'child')类中覆盖的方法的基类版本。

在我们的示例中,我们覆盖了employee类中的set_name()方法。现在我使用了这段代码:

hperson::set_name($new_name);

访问set_name()方法的父类'(person)版本。

代码:

<?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);
        }
    }
?>

注意:使用符号:

::

...允许您专门命名您希望PHP搜索方法的类:

person::set_name() ...告诉PHP在person类中搜索set_name()。

如果您只想通过使用parent关键字引用当前类的父级,还有一个快捷方式。

代码:

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);    
    }   
}
© www.soinside.com 2019 - 2024. All rights reserved.