初始化类php中的实例

时间:2014-06-12 19:29:12

标签: php

如何初始化类中函数内的对象?所以我可以调用每个对象,例如page[0]->getTitle();page[1]->getDescription();

错误现在说:  致命错误:在第92行的C:\ xampp \ htdocs \ class.page.php中不在对象上下文中时使用$ this

的index.php

Page::createObjects();

和我的page.class.php

private title;
private description;
private page;

public function __construct($title="", $description=""){
  $this->title = $title;
  $this->description = $description;
}

public static function createObjects(){
  $database = new database();
  $database->query('SELECT * FROM pages');
  for($i=0;$i<$totalRows;$i++){
    //line 92 here
    $this->page[$i] = new self("Title", "Description");
  }
}

2 个答案:

答案 0 :(得分:2)

您可以考虑在这里使用两个不同的类,Page(page.class.php)和Pages(pages.class.php)。您的Page类将代表数据库中的单个页面记录,而您的Pages类将包含处理一组记录的方法。

page.class.php

<?php
class Page

    private title;
    private description;

    public function __construct($title="", $description=""){
        $this->title = $title;
        $this->description = $description;
    }

    public function getTitle()
    {
        return $this->title;
    }

    public function getDescription()
    {
        return $this->description();
    }
}
?>

pages.class.php

<?php
class Pages

    public static function createObjects(){
        $pages = array();
        $database = new database();
        $database->query('SELECT * FROM pages');
        for($i=0;$i<$totalRows;$i++){
            //line 92 here
            $pages[] = new Page("Title", "Description");
        }

        return $pages;
    }
}
?>

然后在index.php中,您可以从Page获取Pages个对象

的index.php

<?php

$pages = Pages::createObjects();

// to get the title of the first object
echo $pages[0]->getTitle();

?>

答案 1 :(得分:0)

与其他评论者一样,您无法从静态函数

访问$this

静态字段/变量/属性/方法属于该类,而不属于特定实例

<?php

class VIP
{
    private $name, $surname;
    private static $vips = array();

    function __construct($name, $surname){
        $this->name = $name;
        $this->surname = $surname;
        self::$vips[] = $this;
    }

    function getCredentials(){
        return $this->name . ", " . $this->surname;
    }

    static function getAll(){
        return self::$vips;
    }
}

$a = new VIP("George", "Clooney");
$b = new VIP("Scarlet", "Johansson");
$c = new VIP("Brad", "Pitt");
$d = new VIP("Emma", "Stone");

echo $a->getCredentials() . "\n";

foreach(VIP::getAll() as $vip)
    echo $vip->getCredentials() . "\n";

?>

演示:https://eval.in/161874

然而,这很可能是错误的代码,因为你是初学者,你应该忘记静态字段和方法,只使用常规代码


更好的代码:

<?php

class VIP
{
    private $name, $surname;

    function __construct($name, $surname){
        $this->name = $name;
        $this->surname = $surname;
    }

    function getCredentials(){
        return $this->name . ", " . $this->surname;
    }
}

class VIPsList
{
    private $storage = [];

    function add(VIP $vip){
        $this->storage[] = $vip;
    }

    function getAll(){
        return $this->storage;
    }
}

$a = new VIP("George", "Clooney");
$b = new VIP("Scarlet", "Johansson");
$c = new VIP("Brad", "Pitt");
$d = new VIP("Emma", "Stone");

$collection = new VIPsList();
$collection->add($a);
$collection->add($b);
$collection->add($c);
$collection->add($d);

echo $a->getCredentials() . "\n";

foreach($collection->getAll() as $vip)
    echo $vip->getCredentials() . "\n";

?>

https://eval.in/161898