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类中工作_Php_Oop - Fatal编程技术网

数组输出不能从php类中工作

数组输出不能从php类中工作,php,oop,Php,Oop,使用此代码时出错 致命错误:第8行的/home/u448966927/public_html/Myclass.php中只能通过引用传递变量 Myclass.php INDEX.php 将这段代码从 <?php include_once('ytcx.php'); echo getChannels(); ?> 到 从类方法中引用类属性时,需要$this->。通道不存在,在ksort和foreach第22行中无效 改变 ksort(ch

使用此代码时出错

致命错误:第8行的/home/u448966927/public_html/Myclass.php中只能通过引用传递变量

Myclass.php

INDEX.php


将这段代码从

<?php include_once('ytcx.php'); echo getChannels(); ?>

从类方法中引用类属性时,需要$this->。

通道不存在,在ksort和foreach第22行中无效

改变

ksort(channels);
foreach (channels as $channel) {

由于函数getChannels在类中,因此需要。在index.php中添加以下内容:

$objYT = new yt;
echo $objYT->getChannels();
试试这个

class yt
{
    public $channels = array
    (
        "music" => array( "id" => 74,"img" => "<img src=../icon/music.png height=18 width=18 />", "name" => "Music", "slug" => "music") ,
        "sports" => array( "id" => 11,"img" => "<img src=../icon/sport.png height=18 width=18 />", "name" => "Sports", "slug" => "sport"),
        "movies" => array( "id" => 45,"img" => "<img src=../icon/movies.png height=18 width=18 />", "name" => "Movies", "slug" => "movie"),
        "gaming" => array( "id" => 85,"img" => "<img src=../icon/game.png height=18 width=18 />", "name" => "Gaming", "slug" => "game"),
        "people" => array( "id" => 56,"img" => "<img src=../icon/people.png height=18 width=18 />", "name" => "People & Blog", "slug" => "people"),
        "comedy" => array( "id" => 72,"img" => "<img src=../icon/comedy.png height=18 width=18 />", "name" => "Comedy", "slug" => "comedy"),
        "news" => array( "id" => 84,"img" => "<img src=../icon/news.png height=18 width=18 />", "name" => "News & Politics", "slug" => "news"),
        "animation" => array( "id" => 29,"img" => "<img src=../icon/film.png height=18 width=18 />", "name" => "Film & Animation", "slug" => "animation"),
        "auto-vehicles" => array( "id" => 59,"img" => "<img src=../icon/auto.png height=18 width=18 />", "name" => "Autos & Vehicles", "slug" => "auto-vehicles"),
        "howto" => array( "id" => 68,"img" => "<img src=../icon/howto.png height=18 width=18 />", "name" => "Howto & Style", "slug" => "howto"),
        "science" => array( "id" => 98,"img" => "<img src=../icon/science.png height=18 width=18 />", "name" => "Science & Technology", "slug" => "science")

    );

    public function getChannels(){
        $html = "<li><a href='#' class='current'><img src=../icon/pupolar.png height=18 width=18 />\tPopular</a>";
        ksort($this->channels);
        foreach ($this->channels as $channel) {
            $html .="<li><a href='channel/".$channel["slug"]."'>now</a>";
        }
        return $html;
    }
}

您发布的代码中有几个问题

ksort(channels);
foreach (channels as $channel) {
PHP中变量的名称以$开头。当PHP到达通道时,它认为它是使用创建的常量。但函数需要一个通过引用传递的数组类型的参数。这就是错误的来源

如果将其更改为ksort$channels,则仍然是一个错误,因为当前范围中没有定义变量$channels。您可能需要使用当前对象的$channels属性,并且始终使用当前对象的$this来访问该属性,后跟->成员访问操作符和要访问的属性的名称,而不使用$channels:

下一行也一样:

foreach ($this->channels as $channel);
index.php中也存在类似的问题:

文件Myclass.php应该如下所示:

<?php

class yt
{
    // ...

    public function getChannels(){
        $html = "<li><a href='#' class='current'><img src=../icon/pupolar.png height=18 width=18 />\tPopular</a>";
        ksort($this->channels);
        foreach ($this->channels as $channel) {
            $html .="<li><a href='channel/".$channel["slug"]."'>now</a>";
        }
        return $html;
    }
}

最后,我在另一个网站上发布了我的代码,并给出了一个很长的链接,我试图从45 mintsHelp中心发布这个问题:应该有帮助。特别是:但请告诉我如何创建问题tru error error error error error error error error error error error error error error error error error error error当我按“点击pos”时,问题出现您的代码未定义按crtl+k@Epodax现在我的问题是正确的,谢谢你需要创建类yt的对象,比如$yt=newyt;echo$yt->getChannels;正如下面所建议的,您还需要向channels变量添加$@如果我解决了你的问题,你为什么接受另一个答案?没什么大不了的,但对于其他人来说,这个问题看起来有点奇怪。你没有使用$this->来访问类属性,并且在Myclass.php上的一些数组中有拼写错误的slug to sug,在index.php上,你没有创建对象来访问类方法getChannels。就是这样:
$objYT = new yt;
echo $objYT->getChannels();
class yt
{
    public $channels = array
    (
        "music" => array( "id" => 74,"img" => "<img src=../icon/music.png height=18 width=18 />", "name" => "Music", "slug" => "music") ,
        "sports" => array( "id" => 11,"img" => "<img src=../icon/sport.png height=18 width=18 />", "name" => "Sports", "slug" => "sport"),
        "movies" => array( "id" => 45,"img" => "<img src=../icon/movies.png height=18 width=18 />", "name" => "Movies", "slug" => "movie"),
        "gaming" => array( "id" => 85,"img" => "<img src=../icon/game.png height=18 width=18 />", "name" => "Gaming", "slug" => "game"),
        "people" => array( "id" => 56,"img" => "<img src=../icon/people.png height=18 width=18 />", "name" => "People & Blog", "slug" => "people"),
        "comedy" => array( "id" => 72,"img" => "<img src=../icon/comedy.png height=18 width=18 />", "name" => "Comedy", "slug" => "comedy"),
        "news" => array( "id" => 84,"img" => "<img src=../icon/news.png height=18 width=18 />", "name" => "News & Politics", "slug" => "news"),
        "animation" => array( "id" => 29,"img" => "<img src=../icon/film.png height=18 width=18 />", "name" => "Film & Animation", "slug" => "animation"),
        "auto-vehicles" => array( "id" => 59,"img" => "<img src=../icon/auto.png height=18 width=18 />", "name" => "Autos & Vehicles", "slug" => "auto-vehicles"),
        "howto" => array( "id" => 68,"img" => "<img src=../icon/howto.png height=18 width=18 />", "name" => "Howto & Style", "slug" => "howto"),
        "science" => array( "id" => 98,"img" => "<img src=../icon/science.png height=18 width=18 />", "name" => "Science & Technology", "slug" => "science")

    );

    public function getChannels(){
        $html = "<li><a href='#' class='current'><img src=../icon/pupolar.png height=18 width=18 />\tPopular</a>";
        ksort($this->channels);
        foreach ($this->channels as $channel) {
            $html .="<li><a href='channel/".$channel["slug"]."'>now</a>";
        }
        return $html;
    }
}
  include_once('Myclass.php'); 
    $yt = new yt;    
    echo $yt->getChannels();
ksort(channels);
foreach (channels as $channel) {
ksort($this->channels);
foreach ($this->channels as $channel);
<?php include_once('Myclass.php'); echo getChannels();
<?php
include_once 'Myclass.php';
$obj = new yt();
echo $obj->getChannels();
// That's all
<?php

class yt
{
    // ...

    public function getChannels(){
        $html = "<li><a href='#' class='current'><img src=../icon/pupolar.png height=18 width=18 />\tPopular</a>";
        ksort($this->channels);
        foreach ($this->channels as $channel) {
            $html .="<li><a href='channel/".$channel["slug"]."'>now</a>";
        }
        return $html;
    }
}