Visualizzazione dei risultati da 1 a 5 su 5

Discussione: [OOP]classi e funzioni

  1. #1

    [OOP]classi e funzioni

    non so se avete presente il layer di astrazione mysqli, o quello di zend...
    se avete notato funzionano così:
    Codice PHP:
    $db = new mysqli(...dati...);
    $result $db->query(...sql...);
    $array $result->fetch_assoc(); 
    ora se notate, nella variabile $array non metto $db->fetch_assoc, ma metto $result->fetch_assoc, sarebbe come dire $result = $db->query(...sql...)->fetch_assoc().
    è come se dentro la funzione $query, fosse presente un'altra funzione...
    come potrei creare una classe che utilizza lo stesso metodo???
    spero di essere stato chiaro...
    grazie in anticipo a chi risponderà a questa domanda.

  2. #2
    Non è proprio come "$db->query(...sql...)->fetch_assoc()"; è la funzione query che restituisce una classe al posto di una variabile, ad esempio: function query { $x = new classe (); return $x ;};
    Chiamatemi sven se volete non ho voglia di fare una nuova email per una nuova registrazione xD
    Mac Future User , Ventilatore for PC Cooler user , - dry is coming -

  3. #3
    grazie mille... ora tutto torna

  4. #4

    ..............

    Ciao.
    In OOP si chiama composition.
    Eccoti un esempio
    (è un abbozzo non è completa):
    Codice PHP:
    <?php 
    class AbstractDb
    {
        
    //////
        // Legend :
        // private $__varName
        // public  $varName
        /////
        
    var $__host
        var 
    $__user
        var 
    $__password
        var 
    $__database;
        var 
    $conId// connection identifier 
        
    var $result;
        function 
    AbstractDb($options=array())
        {
            if(
    count($options)>0)
            {
                foreach(
    $options as $parameter => $value)
                {
                    (!empty(
    $value))?$this->{$parameter}=$value:$this->isError('Invalid parameter '.$parameter);

                }
                
    $this->conId NULL;
                
    $this->result NULL;
                
    $this->__connectDB();
            }
            else 
            {
                
    $this->isError('No connection parameters were provided');

            }
        }
           function 
    __connectDB()
        {
            if(!
    $this->conId=mysql_connect($this->__host,$this->__user,$this->__password))
            {
                
    $this->isError('Error connecting to the server');
            }
            if(!
    mysql_select_db($this->__database,$this->conId))
            {
                
    $this->isError('Error selecting database'.' '.$this->__database);
            }
        }
        function 
    safeQuery($value)
        {
            if(!
    is_resource($this->conId))
            {
                
    $this->__connectDB();
            }
            if (
    get_magic_quotes_gpc()) 
            {
               
    $value stripslashes($value);
            }
            if (!
    is_numeric($value)) 
            {
               
    $value "'".mysql_real_escape_string($value)."'";
            }
           return 
    $value;
        }
        function 
    performQuery($query)
        {    
            if(!
    is_resource($this->conId))
            {
                
    $this->__connectDB();
            }
            if(!
    $this->result=mysql_query($query,$this->conId))
            {
                
    $this->isError('Error performing query '.$query);
            }
            
    // return new Result object
            
    return new ResultDb($this);
        }
        function 
    getInsertId()
        {
            if(!
    $id=mysql_insert_id($this->conId))
            {
                
    $this->isError('Error getting ID');
            }
            return 
    $id;
        }
        function 
    isError($errorMsg)
        {
                
    $error mysql_errno() . ": " mysql_error() ;
                exit(
    $errorMsg.' '.$error);
        }
        function 
    closeConId()
        {
            
    mysql_close($this->conId);
        }

    }
    //END AbstractDb

    class ResultDb 
    {
        var 
    $mysql// instance of AbstractDb object
        
    function ResultDb(&$mysql)
        {
            
    $this->mysql=&$mysql;
        }
        function 
    fetchObject()
        {
            return 
    mysql_fetch_object($this->mysql->result);
        }
        function 
    fetchRowAss()
        {
             return 
    mysql_fetch_array($this->mysql->result,MYSQL_ASSOC);
        }
        function 
    fetchRowNum()
        {
             return 
    mysql_fetch_array($this->mysql->result,MYSQL_NUM);
        }
        function 
    getNumRows()
        {
            return 
    mysql_num_rows($this->mysql->result);
        }
        
    }
    //END ResultDb

    ?>
    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  5. #5
    grazie mille a tutti e due... era un dubbio che mi assillava, e poi potrebbe sempre tornarmi utile...
    ciao

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.