简体   繁体   中英

Some explanation about calling PHP function in class

I know it is basic php question but I had something that I didn't understand, it is about the return; when I call a function, the bellow code is just an exemple.

Case : In TEST2 when I do my check then I return, that return; do the job and stop the execution of the next code TEST3 Good.

Now in TEST1 I Call a function _checkThat() , and this function do a check then redirect. the problem is it returns shure but the next code will also be executed TEST2 and TEST3 Why ? why when I put the content of that function directly in TEST1 it returns and stop the execution of next code ?

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

Other question, Can I use continue; in this case :

if(isset($name)) {
    Mage::log('check passed'); //This just write in logs
    continue; // or something else
} else if (!isset($name)) {
    // some code
}

Although as already mentioned in comments your example code is not correct, your problem here:

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

is that you do nothing with result returned by _checkThat() . And obviously you should return it:

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

As a sidenote, I should mention that naming methods with _ to mark them protected / private is out-of-date practice since php5.

Here is a simple guide.

Inheritance – reusing code the OOP way

Inheritance is a fundamental capability/construct in Object oriented programming where you can use one class, as the base/basis for another class or many other classes.

Why do it?

Doing this allows you to efficiently reuse the code found in your base class.

Say, you wanted to create a new employee class since we can say that employee is a type/kind of `person', they will share common properties and methods.

Making some sense?

In this type of situation, inheritance can make your code lighter because you are reusing the same code in two different classes. But unlike old-school PHP:

You only have to type the code out once. The actual code being reused, can be reused in many (unlimited) classes but it is only typed out in one place conceptually, this is sort-of like PHP includes().

Take a look at the sample PHP code:

// 'extends' is the keyword that enables inheritance
class employee extends person 
{
    function __construct($employee_name) {
        $this->set_name($employee_name);
    }
}

Reusing code with inheritance:

Because the class employee is based on the class person , employee automatically has all the public and protected properties and methods of 'person'.

Nerd note: Nerds would say that employee is a type of person.

The code:

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

Notice how we are able to use set_name() in employee , even though we did not declare that method in the employee class. That is because we already created set_name() in the class person .

Nerd Note: the person class is called (by nerds,) the base class or the parent class because it is the class that the employee is based on. This class hierarchy can become important down the road when your projects become more complex.

Reusing code with inheritance:

As you can see in the code snippet below, we can call get_name on our employee object, courtesy of person .

The code:

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

This is a classic example of how OOP can reduce the number of lines of code (don't have to write the same methods twice) while still keeping your code modular and much easier to maintain.

Overriding methods 1

Sometimes (when using inheritance,) you may need to change how a method works from the base class.

For example, let us say set_name() method in the 'employee' class, had to do something different than what it does in the person class.

You override the person classes version of set_name(), by declaring the same method in employee .

The code snippet:

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

Notice how set_name() is different in the employee class from the version found in the parent class: person .

< Overriding methods: 2

Sometimes you may need to access your base class's version of a method you overrode in the derived (sometimes called 'child') class.

In our example, we overrode the set_name() method in the employee class. Now I have used this code:

hperson::set_name($new_name);

to access the parent class' (person) version of the set_name() method.

The code:

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

Notes: Using the symbol:

::

… allows you to specifically name the class where you want PHP to search for a method:

person::set_name() … tells PHP to search for set_name() in the person class.

There is also a shortcut if you just want refer to current class's parent – by using the parent keyword.

The code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM