1 (edited by ertys 2014-04-19 11:07:53 PM)

Topic: Question about inherited classes

Hi All!

Attached to the end of the post 3 examples class files. I need to merge these 3 files to work in index.php without the error "Fatal error: Cannot redeclare class xajaxRequestPlugin.......". The example class call.Main.php index.php and work great, but not including the other 2 classes (Module1 and Module2).

As inherit or extend these classes? What is the class that I require in index.php?

All this so that the functions of the 3 classes is available for use in index.php.

Thank you very much for everything!



//    index.php
<?php
    require_once ($_SERVER['DOCUMENT_ROOT'] . '/_core/xajax_core/xajax.inc.php');
    require_once ($_SERVER['DOCUMENT_ROOT'] . '/_includes/_calls/call.Main.php');    
    
    $Main = new Main;
    
    echo '    <html>
                        <head>
                            '. $Main->xajax_js .'
                        </head>
                        <body>
                            <script>xajax_functionMainName1();</script>
                        </body>
                    </html>';
?>
//    call.Main.php
<?php   
class Main
{
        public $xajax, $xajax_js;
        
        public function __construct()
        {
            $this->xajax = new xajax();
            $this->xajax->register(XAJAX_FUNCTION, array('functionMainName1', $this, 'functionMainName1'));
            $this->xajax->register(XAJAX_FUNCTION, array('functionMainName2', $this, 'functionMainName2'));
            $this->xajax->processRequest();
            $this->xajax_js = $this->xajax->getJavascript(BASE_URL ."/_core/");
        }
        
        public function functionMainName1()
        {
            //    Content Funcion
        }
        
        public function functionMainName2()
        {
            //    Content Funcion
        }
}
?>
//    call.module1.php
<?php   
class Module1
{
        public $xajax, $xajax_js;
        
        public function __construct()
        {
            $this->xajax = new xajax();
            $this->xajax->register(XAJAX_FUNCTION, array('functionModule1Name1', $this, 'functionModule1Name1'));
            $this->xajax->register(XAJAX_FUNCTION, array('functionModule1Name2', $this, 'functionModule1Name2'));
            $this->xajax->processRequest();
            $this->xajax_js = $this->xajax->getJavascript(BASE_URL ."/_core/");
        }
        
        public function functionModule1Name1()
        {
            //    Content Funcion
        }
        
        public function functionModule1Name2()
        {
            //    Content Funcion
        }
}
?>
//    call.module2.php
<?php   
class Module2
{
        public $xajax, $xajax_js;
        
        public function __construct()
        {
            $this->xajax = new xajax();
            $this->xajax->register(XAJAX_FUNCTION, array('functionModule2Name1', $this, 'functionModule2Name1'));
            $this->xajax->register(XAJAX_FUNCTION, array('functionModule2Name2', $this, 'functionModule2Name2'));
            $this->xajax->processRequest();
            $this->xajax_js = $this->xajax->getJavascript(BASE_URL ."/_core/");
        }
        
        public function functionModule2Name1()
        {
            //    Content Funcion
        }
        
        public function functionModule2Name2()
        {
            //    Content Funcion
        }
}
?>

Re: Question about inherited classes

Solution php singleton smile

Re: Question about inherited classes

Can you post the details?

If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.

Re: Question about inherited classes

//    index.php
<?php
    require_once ($_SERVER['DOCUMENT_ROOT'] . '/_core/xajax_core/xajax.inc.php');
    require_once ($_SERVER['DOCUMENT_ROOT'] . '/_includes/_class/class.loadXajax.php'); //   singleton

// the includes could be avoided using autoload in php
    require_once ($_SERVER['DOCUMENT_ROOT'] . '/_includes/_calls/call.Main.php');    
    require_once ($_SERVER['DOCUMENT_ROOT'] . '/_includes/_calls/call.Module1.php');    
    require_once ($_SERVER['DOCUMENT_ROOT'] . '/_includes/_calls/call.Module2.php');    
    
    $Main = new Main;
    $Module1 = new Module1;
    $Module2 = new Module2;
    //    unlimited modules :)
    
    echo '    <html>
                        <head>
                            '. xajaxCallBase::getJs() .'
                        </head>
                        <body>
                            <script>xajax_functionMainName1();</script>
                        </body>
                    </html>';
?>
//    class.xajaxCallBase.php (singleton)
<?php 
    class xajaxCallBase
    {
       private static $xajax = null;
       
       private function __construct() {}
    
       private function __clone() {}
         
       public static function getInstance()
       {
          if (self::$xajax === null)
          {
              self::$xajax = new xajax();
            self::$xajax->configure('requestURI', BASE_URL);
            self::$xajax->configure('javascriptURI','_core/');
            self::$xajax->configure('debug', false);
            self::$xajax->configure('decodeUTF8Input', true);
            self::$xajax->setCharEncoding('UTF-8');
            self::$xajax->configure('defaultMode', 'synchronous');
          }
          return self::$xajax;
       }
        
       
       public static function getJs()
       {
           self::$xajax->processRequest();
           return self::$xajax->getJavascript(BASE_URL ."/js/core/");
       }  
    }
?>
//    call.Main.php
<?php   
class Main
{
        public $xajax, $xajax_js;
        
        public function __construct()
        {
            $this->xajax = xajaxCallBase::getInstance();
            
            $this->xajax->register(XAJAX_FUNCTION, array('functionMainName1', $this, 'functionMainName1'));
            $this->xajax->register(XAJAX_FUNCTION, array('functionMainName2', $this, 'functionMainName2'));
        }
        
        public function functionMainName1()
        {
            //    Content Funcion
        }
        
        public function functionMainName2()
        {
            //    Content Funcion
        }
}
?>
//    call.module1.php
<?php   
class Module1
{
        public $xajax, $xajax_js;
        
        public function __construct()
        {
            $this->xajax = xajaxCallBase::getInstance();
            
            $this->xajax->register(XAJAX_FUNCTION, array('functionModule1Name1', $this, 'functionModule1Name1'));
            $this->xajax->register(XAJAX_FUNCTION, array('functionModule1Name2', $this, 'functionModule1Name2'));
        }
        
        public function functionModule1Name1()
        {
            //    Content Funcion
        }
        
        public function functionModule1Name2()
        {
            //    Content Funcion
        }
}
?>
//    call.module2.php
<?php   
class Module2
{
        public $xajax, $xajax_js;
        
        public function __construct()
        {
            $this->xajax = xajaxCallBase::getInstance();
            
            $this->xajax->register(XAJAX_FUNCTION, array('functionModule2Name1', $this, 'functionModule2Name1'));
            $this->xajax->register(XAJAX_FUNCTION, array('functionModule2Name2', $this, 'functionModule2Name2'));
        }
        
        public function functionModule2Name1()
        {
            //    Content Funcion
        }
        
        public function functionModule2Name2()
        {
            //    Content Funcion
        }
}
?>

this would give the expected result:

<script type='text/javascript' charset='UTF-8'>
/* <![CDATA[ */
functionMainName1 = function() { return xajax.request( { xjxfun: 'functionMainName1' } ); };
functionMainName2 = function() { return xajax.request( { xjxfun: 'functionMainName2' } ); };
functionModule1Name1 = function() { return xajax.request( { xjxfun: 'functionModule1Name1' } ); };
functionModule1Name2 = function() { return xajax.request( { xjxfun: 'functionModule1Name2' } ); };
functionModule2Name1 = function() { return xajax.request( { xjxfun: 'functionModule2Name1' } ); };
functionModule2Name2 = function() { return xajax.request( { xjxfun: 'functionModule2Name2' } ); };
/* ]]> */
</script>

Regards!

Re: Question about inherited classes

Thanks much. I'msure this will be of help to a lot of us.

Ed

If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.