小编use*_*027的帖子

完全数组没有返回PHP

我正在尝试创建一个twitter类并创建一个调用该类的方法和属性的对象.基本上我正在做的是为twitter用户名调用数据库并使用结果生成simplexml请求.(我省略了代码的那部分,因为它工作正常).

一切似乎都工作正常,除了我无法弄清楚为什么当我return $this->posts只返回数组的第一个项目时.当我删除return整个数组时返回.我正在使用print_r底部的对象进行测试.

   <?php 
    class twitter { 
        public $xml;
        public $count;
        public $query;
        public $result;
        public $city;
        public $subcategory;
        public $screen_name;
        public $posts;

        public function arrayTimeline(){
            $this->callDb($this->city, $this->subcategory);
            while($row = mysql_fetch_row($this->result)){ 
                foreach($row as $screen_name){ 
                    $this->getUserTimeline($screen_name, $count=2);
                }
                foreach($this->xml as $this->status){
                    return $this->posts[] = array("image"=>(string)$this->status->user->profile_image_url,"name"=>(string)$this->status->name, "username"=>(string)$this->status->user->name, "text"=>(string)$this->status->text, "time"=>strtotime($this->status->created_at)); 
                }
            }
        }


    $test = new twitter;
    $test->city="phoenix";
    $test->subcategory="computers";

    $test->arrayTimeline();

    print_r($test->posts);

    ?>
Run Code Online (Sandbox Code Playgroud)

php arrays oop

0
推荐指数
1
解决办法
329
查看次数

在 PHP 类中创建 cURL 方法

我正在尝试创建一个使用 cURL 向 Twitter API 发出请求的类。我已经对 PHP 中的面向对象编程做了一些研究,但我不太清楚这应该如何工作。以下代码返回 NULL:

 <?php

    class twitter {

      public function curlQuery($url) {
      $ch = curl_init();

      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HEADER, 0);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);

      $json = curl_exec($ch);

      curl_close($ch);

      $array = json_decode($json);
      return var_dump($array);
      }
}

$object1 = new twitter;
$object1->url = "http://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=twitterapi&count=2";
$object1->curlQuery($object1->url);

 ?>
Run Code Online (Sandbox Code Playgroud)

另外,我有点不确定何时在 PHP 类中使用$this->variablevs。$variable一个类中提到的所有变量不都应该被引用为$this->variable吗?为什么你永远不想引用当前对象的变量?

php oop curl

0
推荐指数
1
解决办法
6816
查看次数

使用对象创建 PHP 模板

我正在尝试创建一个包含面向对象代码的 php 模板。这是我正在尝试做的一个简化示例。

索引.php

<?php  require 'requiredfile.php'; ?>

    <html>
      <body>
         <h1><?php echo $object1->variable ?></h1>
      </body>
    </html>
Run Code Online (Sandbox Code Playgroud)

但是如果我想对多个对象使用相同的模板怎么办?我绝对不想为对象 1、2 和 3 创建不同版本的 index.php。我假设我可以做类似的事情$this->variable并传入对象,但我不完全确定如何做到这一点因为它在该类的声明之外。

php oop templates

0
推荐指数
1
解决办法
2971
查看次数

标签 统计

oop ×3

php ×3

arrays ×1

curl ×1

templates ×1