Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 减少类中getter和setter调用的数量_Php_Oop_Getter Setter - Fatal编程技术网

Php 减少类中getter和setter调用的数量

Php 减少类中getter和setter调用的数量,php,oop,getter-setter,Php,Oop,Getter Setter,这可能是一个愚蠢的问题,但我不知道谷歌应该做什么。我正在学习面向对象编程(OOP)并了解其基本概念,但试图实现它,我遇到了一些问题 我正在使用简单的HTMLDOM制作一个web刮板。我想创建一个类,它连接到一个页面,并包含几个不同的函数,我可以在该页面上调用这些函数,具体取决于我想做什么。然后我可以从另一个文件中调用这些类和方法 这是我写的课程: <?php include_once '/simple_html_dom.php'; class CountyScraping { prot

这可能是一个愚蠢的问题,但我不知道谷歌应该做什么。我正在学习面向对象编程(OOP)并了解其基本概念,但试图实现它,我遇到了一些问题

我正在使用简单的HTMLDOM制作一个web刮板。我想创建一个类,它连接到一个页面,并包含几个不同的函数,我可以在该页面上调用这些函数,具体取决于我想做什么。然后我可以从另一个文件中调用这些类和方法

这是我写的课程:

<?php

include_once '/simple_html_dom.php';

class CountyScraping
{
protected $demoMode = 0; //set 1 for demo
protected $cinemas = NULL;
protected $url = NULL;
protected $html = NULL;
protected $counties = array();

function __construct($url)
{
    $this->setUrl($url);
}

public function setUrl($url)
{
    $this->url = $url;
}

public function getUrl()
{
    return $this->url;
}

public function setHtml()
{
    $this->html = file_get_html($this->url);
}

public function getHtml()
{
    return $this->html;
}

public function setCounties()
{
    foreach($this->html->find('#UserLocation option') as $element)
    {
        if (($element->value != NULL) && ($element->plaintext))
        {   
            $county = array();
            $county['id'] = $element->value;
            $county['county_name'] = $element->plaintext;
            $this->counties[] = $county;
        }
    }
}

public function getCounties()
{
    return $this->counties;
}

?>
这很好,我可以继续这样做,但是我觉得我在课外给能手和二传手打了很多电话。我想我应该打一个电话,把一切都安排在课堂上

我的假设正确吗?我的getCountries()方法应该调用我的setHtml和setCountries方法吗?或者我应该尽可能少地使用getter和setter,而是使用另一个函数


欢迎对我的代码的任何部分提出任何建议。

PHP魔术方法也会对您有所帮助。 这里有一个链接,指向
\uu set()

这里还有一篇很好的文章,它将帮助你了解更多有关魔法方法的信息

下面是一个魔术方法的基本示例

<?php
class PropertyTest
{
    /**  Location for overloaded data.  */
    private $data = array();

    /**  Overloading not used on declared properties.  */
    public $declared = 1;

    /**  Overloading only used on this when accessed outside the class.  */
    private $hidden = 2;

    public function __set($name, $value)
    {
        echo "Setting '$name' to '$value'\n";
        $this->data[$name] = $value;
    }

    public function __get($name)
    {
        echo "Getting '$name'\n";
        if (array_key_exists($name, $this->data)) {
            return $this->data[$name];
        }

        $trace = debug_backtrace();
        trigger_error(
            'Undefined property via __get(): ' . $name .
            ' in ' . $trace[0]['file'] .
            ' on line ' . $trace[0]['line'],
            E_USER_NOTICE);
        return null;
    }

    /**  As of PHP 5.1.0  */
    public function __isset($name)
    {
        echo "Is '$name' set?\n";
        return isset($this->data[$name]);
    }

    /**  As of PHP 5.1.0  */
    public function __unset($name)
    {
        echo "Unsetting '$name'\n";
        unset($this->data[$name]);
    }

    /**  Not a magic method, just here for example.  */
    public function getHidden()
    {
        return $this->hidden;
    }
}


echo "<pre>\n";

$obj = new PropertyTest;

$obj->a = 1;
echo $obj->a . "\n\n";

var_dump(isset($obj->a));
unset($obj->a);
var_dump(isset($obj->a));
echo "\n";

echo $obj->declared . "\n\n";

echo "Let's experiment with the private property named 'hidden':\n";
echo "Privates are visible inside the class, so __get() not used...\n";
echo $obj->getHidden() . "\n";
echo "Privates not visible outside of class, so __get() is used...\n";
echo $obj->hidden . "\n";
?>

我建议您在使用OOP进行开发时始终使用SOA(面向服务的体系结构)。使用SOA,来自CountyScraper类的服务应该是唯一的。这就是为什么我们使用了一种新的方法。一个服务不应该阻止一个动作的状态。这就是为什么不应该将url或html设置为属性

$countyScraper = CountyScraper::getInstance();

$countyScraper->findCounties('http://example.com');

首先:将获取页面的作业与解析页面的作业分离。他们是独立的。第二:您不需要在内部使用setter。第三:从API的角度来看-
setHtml
setCountries
方法看起来很奇怪。您应该在
getcountries()方法中使用getter和setter。通常最好将类外getter和setter的使用保持在最低限度。在Yii框架中,setter和getter函数由继承提供。看看Yii,或许可以调整使用的策略。在此处查找更多信息:由于我喜欢属性读取“things”,因此我使用“magic”\uuu get并在其中进行检查,以确保只能读取有效的“property type things”。所以,a+1来自我。”“设置”并不是那么清晰,因为可能需要参数,也可能需要执行其他逻辑。我不明白为什么在这种情况下使用魔术方法。魔法方法有时是有用的,但我建议谨慎使用。记住,为了拥有一个可伸缩、可维护和可测试的应用程序,您应该在类之间使用低耦合(接口)。神奇的呼叫会阻止正确地执行此操作。
Setting 'a' to '1'
Getting 'a'
1

Is 'a' set?
bool(true)
Unsetting 'a'
Is 'a' set?
bool(false)

1

Let's experiment with the private property named 'hidden':
Privates are visible inside the class, so __get() not used...
2
Privates not visible outside of class, so __get() is used...
Getting 'hidden'


Notice:  Undefined property via __get(): hidden in <file> on line 70 in <file> on line 29
<?php

include_once '/simple_html_dom.php';

class CountyScraper
{
    /**
     * Returns the *Singleton* instance of this class.
     *
     * @staticvar Singleton $instance The *Singleton* instances of this class.
     *
     * @return Singleton The *Singleton* instance.
     */
    public static function getInstance()
    {
        static $instance = null;
        if (null === $instance) {
            $instance = new static();
        }

        return $instance;
    }

    /**
     * Protected constructor to prevent creating a new instance of the
     * *Singleton* via the `new` operator from outside of this class.
     */
    protected function __construct()
    {
    }

    /**
     * Private clone method to prevent cloning of the instance of the
     * *Singleton* instance.
     *
     * @return void
     */
    private function __clone()
    {
    }

    /**
     * Private unserialize method to prevent unserializing of the *Singleton*
     * instance.
     *
     * @return void
     */
    private function __wakeup()
    {
    }

    public function findCounties($url)
    {
        $counties = array();
        $html = file_get_html($url);

        foreach ($html->find('#UserLocation option') as $element)
        {
            if (($element->value != NULL) && ($element->plaintext))
            {   
                $county = array();
                $county['id'] = $element->value;
                $county['county_name'] = $element->plaintext;
                $counties[] = $county;
            }
        }
    }
}
$countyScraper = CountyScraper::getInstance();

$countyScraper->findCounties('http://example.com');