>  기사  >  백엔드 개발  >  PHP如何获取对象里的一个方法或者属性

PHP如何获取对象里的一个方法或者属性

WBOY
WBOY원래의
2016-06-23 13:07:231828검색

require_once '../PHPWord.php';$PHPWord = new PHPWord();$document = $PHPWord->loadTemplate('Template.docx');print_r($document);exit;

上面是phpword类,我现在想输出word文档的内容,我打印了一下document ,输出的是如下内容:

可以看到_documentXML:PHPWord_Template:private  这个里面输出的就是我想要的,不知道如何用php调用到这个方法。求大神指教!


回复讨论(解决方案)

但那是私有的属性
在这个类中,一定有某个方法是用来操作这个属性的

但那是私有的属性
在这个类中,一定有某个方法是用来操作这个属性的


谢谢,下面是这个类的文件,能不能帮忙看一下如何调用啊<?php/** * PHPWord * * Copyright (c) 2011 PHPWord * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA * * @category   PHPWord * @package    PHPWord * @copyright  Copyright (c) 010 PHPWord * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL * @version    Beta 0.6.3, 08.07.2011 *//** * PHPWord_DocumentProperties * * @category   PHPWord * @package    PHPWord * @copyright  Copyright (c) 2009 - 2011 PHPWord (http://www.codeplex.com/PHPWord) */class PHPWord_Template {        /**     * ZipArchive     *      * @var ZipArchive     */    private $_objZip;        /**     * Temporary Filename     *      * @var string     */    private $_tempFileName;        /**     * Document XML     *      * @var string     */    private $_documentXML;            /**     * Create a new Template Object     *      * @param string $strFilename     */    public function __construct($strFilename) {        $path = dirname($strFilename);        $this->_tempFileName = $path.DIRECTORY_SEPARATOR.time().'.docx';                copy($strFilename, $this->_tempFileName); // Copy the source File to the temp File        $this->_objZip = new ZipArchive();        $this->_objZip->open($this->_tempFileName);                $this->_documentXML = $this->_objZip->getFromName('word/document.xml');    }        /**     * Set a Template value     *      * @param mixed $search     * @param mixed $replace     */    public function setValue($search, $replace) {        if(substr($search, 0, 2) !== '${' && substr($search, -1) !== '}') {            $search = '${'.$search.'}';        }                if(!is_array($replace)) {            $replace = utf8_encode($replace);        }                $this->_documentXML = str_replace($search, $replace, $this->_documentXML);    }        /**     * Save Template     *      * @param string $strFilename     */    public function save($strFilename) {        if(file_exists($strFilename)) {            unlink($strFilename);        }                $this->_objZip->addFromString('word/document.xml', $this->_documentXML);                // Close zip file        if($this->_objZip->close() === false) {            throw new Exception('Could not close zip file.');        }                rename($this->_tempFileName, $strFilename);    }}?>

    public function save($strFilename) {        if(file_exists($strFilename)) {            unlink($strFilename);        }                 $this->_objZip->addFromString('word/document.xml', $this->_documentXML);                 // Close zip file        if($this->_objZip->close() === false) {            throw new Exception('Could not close zip file.');        }                 rename($this->_tempFileName, $strFilename);    }
这个方法就是保存到文件
如果你想要不同的功能,可以给这个类加个方法

    public function save($strFilename) {        if(file_exists($strFilename)) {            unlink($strFilename);        }                 $this->_objZip->addFromString('word/document.xml', $this->_documentXML);                 // Close zip file        if($this->_objZip->close() === false) {            throw new Exception('Could not close zip file.');        }                 rename($this->_tempFileName, $strFilename);    }
这个方法就是保存到文件
如果你想要不同的功能,可以给这个类加个方法



    public function show() {        if(file_exists($strFilename)) {            unlink($strFilename);        }        return $this->_documentXML;    }	


谢谢您提醒,建立这个方法可以了,感谢!

        if(file_exists($strFilename)) {
            unlink($strFilename);
        }
这个不要!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:ThinkPHP惯例配置讲解다음 기사:PHP木马清理过程