PHP - 如何使用另一个目录文件中的函数?

PHP - How to use functions from another directory file?

我在$root/content/plugins/musicplayer/includes/player.php

中有这个功能
public function head_script( $id, $playlist_id, $songs, $in_popup, $autoplay = false ) {
        $output   = '';
        $playlist = '';
        $artist   = '';
        $free     = null;
        $external = 0;

        if ( $songs ) {

            $ogg = '';

            foreach ( $songs as $song ) {

                $free = $song->free;

                if ( $song->poster ) {
                    $poster = esc_url( $song->poster );
                } else {
                    $poster = $this->get_default_playlist_poster( $playlist_id );
                }

                $playlist .= '{  title : "' . $song->name . '", mp3:"'. esc_url( $song->mp3 ) .'"';

                if ( $song->artist )
                    $playlist .= ', artist : "' . $song->artist . '" ';


                if ( $free != 'on' ) {

                $playlist .= ',poster : "' . $poster . '" ';


                $playlist .= ' },';
            }

            $playlist = substr( $playlist, 0, -1 );

            $output .= '<script type="text/javascript">//<![CDATA[';

            $output .= "\n";
            $output .= 'jQuery(document).ready(function($) {
                    new jPlayerPlaylist( {
                        jPlayer: "#jquery_jplayer_' . $id . '",
                        cssSelectorAncestor: "#jp_container_' . $id . '" }, 
                        ['.$playlist.'], {
                        swfPath: "' . WOLF_JPLAYER_PLUGIN_URL . '/assets/js/src",
                        wmode: "window", ';

            $output .= '});'; // end playlist

            if ( ! $in_popup )
                $output .= $this->popup();

            $output .= '});'; // end document ready playlist

            $output .= '//]]></script>';
        }

        echo $output;
    }

如何在 $root/content/themes/bigwolf/index.php 中使用它,并且仍然能够毫无问题地调用本机目录中最初正常调用的所有函数?

你可以包含它。这就是你的做法。

f1.php

<?php

    function func1()
    {
        echo 'hi';
    }

f2.php

<?php

    require_once('f1.php'); // require
    //include 'f1.php'; // or include

    func1();

很明显musicplayer是一个插件。如果格式正确,WordPress 将自动包含它。所以在你的插件的主文件中放

require_once __DIR__ . "/includes/player.php';

这将使您的 player.php 进入并让您访问其中的功能。

HTH,

=C=