oop面向对象编程
Object Oriented Programming is by far the best way to program. Now, procedural programming does have its place. For example, if you just needed a simple form submission, you wouldn't want to go through all the work in creating a ton of classes for that little job. But if you are:
到目前为止,面向对象编程是最好的编程方法。 现在,过程编程确实占有一席之地。 例如,如果您只需要简单的表单提交,那么您将不需要完成为该小工作创建大量类的所有工作。 但是,如果您是:
1) Programming an application that might expand or add new features
1)对可能会扩展或添加新功能的应用程序进行编程
2) Programming an application that was written procedurally and is a big mess, or
2)对程序编写的应用程序进行编程,并且会造成很大的麻烦,或者
3) Just wanting to convert to the best programming practices the world has ever known
3)只想转换为世界上已知的最佳编程实践
then Object Oriented Programming (OOP) is for you!
那么面向对象编程(OOP)就适合您!
OOP is a concept fairly new to PHP. It was introduced in version 4 as an afterthought, and wasn't well designed. Then in PHP5, OOP really started taking a hold of things, providing a good amount of room to use OOP. Now, as we look toward the future, PHP6 should have fairly complete support for OOP. But at this moment, we will embrace OOP with version 5.
OOP是PHP相当新的概念。 后来,它是在版本4中引入的,并且设计得不好。 然后在PHP5中,OOP真正开始掌握一些东西,为使用OOP提供了足够的空间。 现在,当我们展望未来时,PHP6应该对OOP有相当完整的支持。 但是目前,我们将在版本5中使用OOP。
To jump-start your life into OOP, we'll just start with the basics. The heart and soul of OOP is a Class. Think of a class as a robot. It has a blueprint on how its designed and built. That's what we create, the blueprint that PHP interprets. We start by letting PHP know that we want to create the blueprints for a class like so:
要开始使用OOP,我们将从基础知识开始。 OOP的心脏和灵魂是一类。 将一堂课看作是机器人。 它具有如何设计和构建蓝图。 这就是我们创建的,PHP解释的蓝图。 首先,让PHP知道我们要为此类创建蓝图:
This simply means that we want to create a SimpleRobot. This SimpleRobot can't do anything, though! Let's rewrite our blueprint to allow him to walk:
这只是意味着我们要创建一个SimpleRobot。 但是,此SimpleRobot无法执行任何操作! 让我们重写我们的蓝图,让他走路:
Now, I'm guessing that you know how to create a function, right? Well that's all a class is! Its a container for variables and functions! Let's give our robot some knowledge by adding some variables!
现在,我猜您知道如何创建函数,对吗? 好吧,这就是一堂课! 它是变量和函数的容器! 让我们通过添加一些变量来给我们的机器人一些知识!
'0', 'y'=>'0');
function walk($direction){}
}
?>
Simple right? But how do we actually use the robot? Well, we have to let PHP know to create a new SimpleRobot:
简单吧? 但是,我们实际上如何新的 SimpleRobot:
Wait a minute! That looks like a function! But what's with that new keyword there? Well, its not a function, trust me. The new keyword lets the PHP parser know that we want to create a New copy of something, being whatever follows the keyword. The parenthesis after the class name allows you to pass arguments when the class is created. We'll get to that, but lets focus on actually using the class first.
等一下! 看起来像个函数! 但是那里的新关键字是什么呢? 好吧,这不是功能,相信我。 new关键字使PHP解析器知道我们想要创建某个内容的
Now that we have created an Object, we can start playing around with it! A copy of the class is now stored in the $robot variable. To access different parts of the class, we use the dash and greater than keys: ->. Think of it as an arrow pointing to PHP what we wanting to use out of the variable. So, if we wanted to move the robot, we would do it like so:
现在我们已经创建了一个
walk("left");
?>
So we are telling PHP to look for a function (the walk function) and call it, passing it the "left" argument. What if we want the value of a variable stored in the class? Easy. Use the same symbol as before:
因此,我们告诉PHP寻找一个函数(walk函数)并调用它,并传递“ left”参数。 如果我们希望将变量的值存储在类中怎么办? 简单。 使用与以前相同的符号:
power;
?>
This will output, "Electricity". But wait! We didn't put the $ sign before the word "power"! That's right! PHP knows that you have to either be calling for a function or a variable. Since we didn't put the parenthesis after the name, it knew we must want a variable. Okay, that's all well and nice, but what about working with variables and functions Inside the class? Well, it's just about the same way, but slightly different. Since we want to refer to the object we are currently using, we need a way to refer to that specific object. Well, how about referring to $this object!
这将输出“电力”。 可是等等! 我们没有在“电源”一词前加$符号! 那就对了! PHP知道您必须要调用函数或变量。 由于我们没有在名称后加上括号,因此它知道我们必须要有一个变量。 好的,一切都很好,但是在类
0, 'y'=>0);
function walk($direction) {
switch($direction){
case "left":
$this->location['x'] -= 1;
break;
case "right":
$this->location['x'] += 1;
break;
case "forward":
$this->location['y'] += 1;
break;
case "backward":
$this->location['y'] -= 1;
break;
}
}
}
?>
So now we see that by specifying $this we tell PHP to work with the current object that we called the function from. Let's see how this works with multiple robots to get the idea:
因此,现在我们看到通过指定$ this,我们告诉PHP使用当前调用函数的对象。 让我们看看这与多个机器人如何协同工作以产生想法:
walk("forward");
$robot2->walk("backward");
echo $robot1->location['y']; //outputs 1
echo $robot2->location['y']; //outputs -1
?>
Now do you understand how classes work? Each variable holds an object specified by the class you stated. Each variable holds data for that object that is specific to THAT object, and THAT object only. This way you could create a whole bunch of objects each with their own data:
现在您了解课程的工作原理了吗? 每个变量都包含您所声明的类指定的对象。 每个变量都保存该对象的数据,该数据特定于THAT对象,并且仅限于THAT对象。 这样,您可以创建一堆对象,每个对象都有自己的数据:
10000;$i++){
$robot$i = new SimpleRobot();
$robot$i->location['x'] = rand(1, 999);
$robot$i->location['y'] = rand(1, 999);
}
?>
OK, one last thing. I told you that you can pass arguments to the class when you construct it right? Well how on earth do we do this then?! PHP has some magic functions that you can override and build yourself. One of them, as we can guess, is the __construct() function (note: that is two underscores before construct):
好,最后一件事。 我告诉过您,您可以在构造类时将参数传递给类吗? 那么我们到底该怎么做呢? PHP具有一些魔术函数,您可以覆盖它们并自行构建。 我们可以猜测,其中之一是__construct()函数(注意:在构造之前有
location['x'] = $x;
$this->location['y'] = $y;
}
function walk($direction) {
switch($direction){
case "left":
$this->location['x'] -= 1;
break;
case "right":
$this->location['x'] += 1;
break;
case "forward":
$this->location['y'] += 1;
break;
case "backward":
$this->location['y'] -= 1;
break;
}
}
}
?>
This function is one of the most useful functions you will ever use. So now you know the basics of OOP in PHP! Now, start toward the path of ultimate programming and enjoy!
此功能是您将要使用的最有用的功能之一。 因此,现在您了解了PHP中OOP的基础知识! 现在,踏上终极编程之路,尽情享受吧!
翻译自: https://www.experts-exchange.com/articles/2232/Object-Oriented-Programming-OOP-in-PHP-The-Class.html
oop面向对象编程