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

php中文网
发布: 2016-06-23 13:07:23
原创
1979人浏览过

require_once '../phpword.php';$phpword = new phpword();$document = $phpword->loadtemplate('template.docx');print_r($document);exit;
登录后复制

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

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

如此AI员工
如此AI员工

国内首个全链路营销获客AI Agent

如此AI员工 71
查看详情 如此AI员工


回复讨论(解决方案)

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

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


谢谢,下面是这个类的文件,能不能帮忙看一下如何调用啊<?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);
        }
这个不要!

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号