Ver Mensaje Individual
  #4 (permalink)  
Antiguo 20/06/2016, 17:24
Avatar de ajalex
ajalex
 
Fecha de Ingreso: agosto-2003
Mensajes: 122
Antigüedad: 23 años, 2 meses
Puntos: 0
Respuesta: Cargar Class con Propiedades?

Disculpa duplicar el tema pero no vi respuesta (no me llego mail y no revise antes aqui) que me respondiste.

Como mencionabas en mi post duplicado formule mal mi pregunta y no me dí a entender

Tengo mi sitio modular el cual usa url amigables, y modulos adicionales y cada uno sus propias clases.

Pero mi clase principal Site, es la que llama a la configuración de el sitio y funciones genéricas que ocuparía en todo el sitio con clases externas.

Mi pregunta creo que seria eso como puedo acceder a mis funciones desde clases externas, previamente la clase Site esta instanciada al inicio siempre para poder saber que tipo de solicitud estoy haciendo además de las funciones genéricas y smarty.

Después de lo que me explicas busque y (lo mas seguro no entendi) los ejemplos no me funcionanl del todo

inicie con singleton y quedo mi Site.php

Código PHP:
class Site {

    public    
$system = array();
    public     
$db;
    public     
$prefix;
    private static 
$instancia;

    public function 
__construct() {

            
$this->db = new SQL(DB_HOST, DB_USER, DB_PASS, DB_NAME);
            
$this->prefix = DB_PREFIX;

            
$NVConf = $this->db->get_results("SELECT * FROM {$this->prefix}config");

               foreach (
$NVConf as $systenconfig) {
                
$this->system[$systenconfig['option']] = $systenconfig['value'];
            } 

    }

    public static function 
Instance() {

              if (  !
self::$instancia instanceof self)
              {
                 
self::$instancia = new self;
              }
              return 
self::$instancia;
       }

    public function 
System( $data = '' ) {

            if ( 
$data == "" ) {
                
$system = $this->system;

            } else {
                
$system = $this->system[ strtolower( $data ) ];
            }

            return 
$system;
    }

    public function 
UrlSite(){

        
$URL = ($this->System("site_siteurlssl")!='') ? $this->System("site_siteurlssl"):$this->System("site_siteurl");
    
        return 
$URL;
    }
    public function 
IsPage($slug){

            
$slug =  $this->db->filter($slug);

            
$very = "SELECT * FROM `{$this->prefix}posts` WHERE published='1' and permalink='{$slug}'";
            
$very2 = "SELECT * FROM `{$this->prefix}posts_cat` WHERE permalink='{$slug}' and type='posts'";

            if(
$this->db->num_rows($very)!=0 || $this->db->num_rows($very2)!=0 || is_numeric($slug)) {
                
$result = true;
            } else {
                
$result = false;
            }

        return 
$result;
    }
...............................
etc 
Eso sería mi clase Site, mi index.php

Código PHP:
use classesSite;
use 
classesMenus;

    
$Site   = Site::Instance();
    
$Site->Smarty = new SmartyBC;
    
$Site->Menu = new Menus;

    
// listar menus y Active URL
    
$CurrentUrl = $Site->SystemURL( "current" );

    
$CurrentActive = $Site->Menu->GetCurrent( $CurrentUrl );

    

    
$ListMenus = $Site->db->get_results( "SELECT `cid`, `title`, `goto` FROM `{$Site->prefix}menu` WHERE published='1' " );
    foreach ( 
$ListMenus as $row ) {

        
$Site->Smarty->assign( "Menu_" . $row[ "goto" ] . "", $row[ "title" ] );
        
$Site->Smarty->assign( "MENU" . $row[ "goto" ], $Site->Menu->menu( $row[ "cid" ], $CurrentActive, 0, 0 ) );
    }

    if ( 
$Site->SystemURL( "0" ) == "" or $Site->SystemURL( "0" ) == "index.html" ) {
        include( 
ROOT_PATH . "/modules/home/index.php" );
    } else {
        
//Antihack
        
$page = $Site->filter( $Site->SystemURL( "0" ), "injection" );

        if ( 
file_exists( ROOT_PATH . "/modules/{$page}/index.php" ) ) {

            include( 
ROOT_PATH . "/modules/{$page}/index.php" );

        } else if ( 
$Site->IsPage( $page ) ) {

            include( 
ROOT_PATH . "/modules/posts/index.php" );

        } elseif ( 
$page == 'archives' ) {

            include( 
ROOT_PATH . "/modules/posts/index.php" );

        } else {

            
$Site->ErrorPage();
        }
    } 
Ahora como sería posible que en mi clase Menus.php accediera a las funciones de mi clase Site que en este primer caso necesito la conexión a mysql y mi prefix para la db que la funcion que es GetCurrent es:

Menus.php
Código PHP:
    public function GetCurrent($CurrentUrl){
        

        
$CurrentUrl = (empty($CurrentUrl) or $CurrentUrl=="index.html") ? "home":$CurrentUrl;

        
$modulo = "SELECT cid FROM `{$this->prefix}mod` WHERE mod_permalink='{$CurrentUrl}' LIMIT 1";

        
$post = "SELECT cid, type FROM {$this->prefix}posts where permalink='{$CurrentUrl}' LIMIT 1";

        
$cat = "SELECT cid FROM {$this->prefix}posts_cat where permalink='{$CurrentUrl}' LIMIT 1";

            
$result = array();

        if(
$this->db->num_rows($modulo)!=0) {
                
$row = $this->db->fetch_array($modulo);
                
$result = $this->GetMenuParents($row['cid'],"module");
        } else if(
$this->db->num_rows($post)!=0) {
                
$row = $this->db->fetch_array($post);
                
$result = $this->GetMenuParents($row['cid'],$row['type']); 
        } else if(
$this->db->num_rows($cat)!=0) {
                
$row = $this->db->fetch_array($cat);
                
$result = $this->GetMenuParents($row['cid'],"category");
        } else {

        }

        return 
$this->array_values_recursive($result);
    } 
Y en un futuro en mis módulos como lo muestro arriba serian clases que requerirían la conexión a la db y funciones de Site.php, antes con funciones normales con el global solucionaba todo pero aqui en OOP por lo que es no es viable usar el global