php oop如何使用属性和方法构成一个类 [英] php oop how to work with properties and methods form a class

查看:78
本文介绍了php oop如何使用属性和方法构成一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是php oop的新手,我对类有一些了解,但是我仍然不了解如何在创建的Object中使用其属性和方法.我需要了解下面报告的代码是否正确,否则我做错了.

I am new to php oop, I have a some idea re Classes but I still don't get around how to use its properties and methods in a created Object. I need to understand if the code reported below is correct, and if not what I do wrong.

我假设我有一个可以为我做任何事情的班级.我们称之为Class myClass {....}

I am assuming that I have a Class that can do anything for me. Let's call it Class myClass {....}

现在,我从中创建一个对象,并尝试使用其方法和属性,如下所示:-

Now I create an object from it and try to work with its methods and properties like this:-

$myObject = new myClass;

$myObject->checkSpeedLight(); // method for checking the speed

if($this->lightSpeed > 10000) echo ("slow down!"); // property defined with a value of 10000

if($this->lightSpeed =< 10000) echo ("Speed up!);

$myObject->keepLightingUp();

$myObject->sleep();

echo ("ligth up");

我知道这毫无意义,只是一个例子.我需要了解的是编写的方式是否正确;任何帮助表示赞赏.

It has no sense I know, it is just an example. What I need to understand is if the way is written is correct; Any help appreciated.

推荐答案

$this不在上下文中,只能在类定义中使用(在内部方法等内部).

$this is out of context, it can only be used from within the class definition (inside of internal methods etc).

在函数之外,我们使用$myObject->lightspeed;

Outside of the function, we use $myObject->lightspeed;

此外,我假设您正在使用checkLightSpeed()方法设置lightspeed属性.

Also, I'm assuming that you are setting the lightspeed property with the checkLightSpeed() method.

编辑!
此外,使用 getter和setter 方法被认为是一种很好的做法.关键是不要直接访问您的属性,而要通过抽象层方法.

EDIT!
Additionally, it's considered good practice to have a getter and setter methods. The point is to not access your properties directly, but through an abstraction layer method.

class MyClass {
    private $property = "Hello World!";

    public function getProperty() {
        return $this->property;
    }
}

$obj = new MyClass();
$obj->getProperty();

通过这种方式,您可以更好地控制查看属性的方式,方式和时间(例如,您可能需要数据库连接才能查看属性或限制访问权限).

This way you have more control over what, how and when you view your property (for instance, you can require a database connection in order to view it, or restrict access).

还请注意,该属性本身被声明为 private ,因此从类外部无法直接访问.

Also note that the property itself is declared private, so direct access from outside the class's guts is restricted.

例如,这是我用PHP制作的SpeedOfLight类:

For instance, this is my SpeedOfLight class made in PHP:

<?php

    /**
     * @class                  SpeedOfLight
     *
     * @property $speedOfLight private
     *
     */
    class SpeedOfLight {

        private $speedOfLight;

        public function checkSpeedOfLight() {
            $this->speedOfLight = 300000000;
        }

        public function getSpeedOfLight() {
            return $this->speedOfLight;
        }

    }

    #Begin testing!
    $obj = new SpeedOfLight();
    $obj->checkSpeedOfLight();

    if ($obj->getSpeedOfLight() <= 100000000) {
        echo "Speed up!";
    }
    elseif ($obj->getSpeedOfLight() >= 350000000) {
        echo "Slow down!";
    }
    else {
        echo "Just right!";
    }

(我不知道keepLightSpeed()sleep()是什么,所以我省略了它们,但这是关键部分).

(I didn't know what keepLightSpeed() or sleep() were so I omitted them, but this is the key part).

除此之外,你还不错.

这篇关于php oop如何使用属性和方法构成一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆