关于在 class 中调用 PHP 函数的一些解释
Some explanation about calling PHP function in class
我知道这是基本的 php 问题,但我有一些我不明白的东西,它是关于 return;
当我调用一个函数时,下面的代码只是一个例子。
案例: 在 TEST2
中,当我进行检查时,我 return,return;
完成工作并停止执行下一个代码TEST3
好
现在 TEST1
我调用了一个函数 _checkThat()
,这个函数进行检查然后重定向。问题是 return 确实如此,但下一个代码也将被执行 TEST2
和 TEST3
为什么?为什么当我将该函数的内容直接放在 TEST1
中时 returns 并停止执行下一个代码?
<?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;
}
}
}
其他问题,我可以在这种情况下使用 continue;
:
if(isset($name)) {
Mage::log('check passed'); //This just write in logs
continue; // or something else
} else if (!isset($name)) {
// some code
}
尽管评论中已经提到您的示例代码不正确,但您的问题在这里:
if(isset($name)) {
$this->_checkThat($name);
}
是你对_checkThat()
返回的结果什么都不做。显然你应该 return
它:
if(isset($name)) {
return $this->_checkThat($name);
}
作为旁注,我应该提到使用 _
来标记它们 protected
/private
的命名方法自 php5 以来已经过时了。
这里有一个简单的指南。
Inheritance – reusing code the OOP way
继承是面向对象编程的基础capability/construct,您可以在其中使用一个 class,作为另一个 class 或许多其他 class 的 base/basis ]es.
为什么要这样做?
这样做可以让您有效地重用在您的库中找到的代码 class。
说,你想创建一个新的 employee class
因为我们可以说 employee
是一个 type/kind 的“人”,他们将共享共同的属性和方法。
有点道理?
在这种情况下,继承可以使您的代码更轻便,因为您在两个不同的 class 中重用相同的代码。但不像 old-school
PHP:
您只需输入一次代码。
被重用的实际代码,可以在许多(无限)classes 中重用
但它只是在概念上在一个地方打出来的,这有点像
像 PHP includes().
看看示例PHP代码:
// 'extends' is the keyword that enables inheritance
class employee extends person
{
function __construct($employee_name) {
$this->set_name($employee_name);
}
}
Reusing code with inheritance:
因为 class employee
是基于 class person
, employee
自动拥有所有 public 和受保护的属性和'人'的方法。
书呆子注:书呆子会说employee
是一种人。
代码:
class employee extends person
{
function __construct($employee_name){
$this->set_name($employee_name);
}
}
请注意我们如何能够在 employee
中使用 set_name(),即使我们没有在 employee
class 中声明该方法。那是因为我们已经在 class person
.
中创建了 set_name()
书呆子注意:person
class 被(书呆子)称为 base
class 或 parent
class 因为employee
是基于 class。当您的项目变得更加复杂时,这个 class 层次结构在以后会变得很重要。
Reusing code with inheritance:
正如您在下面的代码片段中看到的,我们可以在 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();
?>
这是一个 classic 示例,说明 OOP 如何减少代码行数(不必将相同的方法编写两次)同时保持代码模块化并更易于维护。
Overriding methods 1
有时(在使用继承时)您可能需要从基础 class.
更改方法的工作方式
例如,让我们说 'employee' class 中的 set_name() 方法必须做一些与 person
[=] 中不同的事情136=].
您 override
person
classes 版本的 set_name(),通过在 employee
.
中声明相同的方法
代码片段:
<?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;
}
}
}
?>
请注意 set_name() 在 employee
class 中与父 class 中的版本有何不同:person
.
<覆盖方法:2
有时您可能需要访问您在派生(有时称为“子”)中覆盖的方法的基础 class 版本 class。
在我们的示例中,我们覆盖了 employee
class 中的 set_name() 方法。我现在有
使用此代码:
hperson::set_name($new_name);
访问父 class'(人)版本的 set_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);
}
}
?>
注意事项:使用符号:
::
... 允许您在希望 PHP 搜索方法的地方明确命名 class:
person::set_name()
... 告诉 PHP 在 person
class.
中搜索 set_name()
如果你只想引用当前 class 的父级,还有一个快捷方式——使用 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);
}
}
我知道这是基本的 php 问题,但我有一些我不明白的东西,它是关于 return;
当我调用一个函数时,下面的代码只是一个例子。
案例: 在 TEST2
中,当我进行检查时,我 return,return;
完成工作并停止执行下一个代码TEST3
好
现在 TEST1
我调用了一个函数 _checkThat()
,这个函数进行检查然后重定向。问题是 return 确实如此,但下一个代码也将被执行 TEST2
和 TEST3
为什么?为什么当我将该函数的内容直接放在 TEST1
中时 returns 并停止执行下一个代码?
<?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;
}
}
}
其他问题,我可以在这种情况下使用 continue;
:
if(isset($name)) {
Mage::log('check passed'); //This just write in logs
continue; // or something else
} else if (!isset($name)) {
// some code
}
尽管评论中已经提到您的示例代码不正确,但您的问题在这里:
if(isset($name)) {
$this->_checkThat($name);
}
是你对_checkThat()
返回的结果什么都不做。显然你应该 return
它:
if(isset($name)) {
return $this->_checkThat($name);
}
作为旁注,我应该提到使用 _
来标记它们 protected
/private
的命名方法自 php5 以来已经过时了。
这里有一个简单的指南。
Inheritance – reusing code the OOP way
继承是面向对象编程的基础capability/construct,您可以在其中使用一个 class,作为另一个 class 或许多其他 class 的 base/basis ]es.
为什么要这样做?
这样做可以让您有效地重用在您的库中找到的代码 class。
说,你想创建一个新的 employee class
因为我们可以说 employee
是一个 type/kind 的“人”,他们将共享共同的属性和方法。
有点道理?
在这种情况下,继承可以使您的代码更轻便,因为您在两个不同的 class 中重用相同的代码。但不像 old-school
PHP:
您只需输入一次代码。 被重用的实际代码,可以在许多(无限)classes 中重用 但它只是在概念上在一个地方打出来的,这有点像 像 PHP includes().
看看示例PHP代码:
// 'extends' is the keyword that enables inheritance
class employee extends person
{
function __construct($employee_name) {
$this->set_name($employee_name);
}
}
Reusing code with inheritance:
因为 class employee
是基于 class person
, employee
自动拥有所有 public 和受保护的属性和'人'的方法。
书呆子注:书呆子会说employee
是一种人。
代码:
class employee extends person
{
function __construct($employee_name){
$this->set_name($employee_name);
}
}
请注意我们如何能够在 employee
中使用 set_name(),即使我们没有在 employee
class 中声明该方法。那是因为我们已经在 class person
.
书呆子注意:person
class 被(书呆子)称为 base
class 或 parent
class 因为employee
是基于 class。当您的项目变得更加复杂时,这个 class 层次结构在以后会变得很重要。
Reusing code with inheritance:
正如您在下面的代码片段中看到的,我们可以在 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();
?>
这是一个 classic 示例,说明 OOP 如何减少代码行数(不必将相同的方法编写两次)同时保持代码模块化并更易于维护。
Overriding methods 1
有时(在使用继承时)您可能需要从基础 class.
更改方法的工作方式例如,让我们说 'employee' class 中的 set_name() 方法必须做一些与 person
[=] 中不同的事情136=].
您 override
person
classes 版本的 set_name(),通过在 employee
.
代码片段:
<?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;
}
}
}
?>
请注意 set_name() 在 employee
class 中与父 class 中的版本有何不同:person
.
<覆盖方法:2
有时您可能需要访问您在派生(有时称为“子”)中覆盖的方法的基础 class 版本 class。
在我们的示例中,我们覆盖了 employee
class 中的 set_name() 方法。我现在有
使用此代码:
hperson::set_name($new_name);
访问父 class'(人)版本的 set_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);
}
}
?>
注意事项:使用符号:
::
... 允许您在希望 PHP 搜索方法的地方明确命名 class:
person::set_name()
... 告诉 PHP 在 person
class.
如果你只想引用当前 class 的父级,还有一个快捷方式——使用 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);
}
}