Visualizzazione dei risultati da 1 a 3 su 3

Hybrid View

  1. #1

    Creazione dinamica do oggetti nelle classi

    Ciao,
    sarebbe possibile secondo voi lavorando su metodi __set e __get fare una cos del genere?

    $myClass=new MyClass();
    $myClass->obj1->obj2->property='valore';
    $myClass->obj1->property1='valore 1';

    Vorrei venisse fuori una cosa del genere

    MyClass Object
    (
    [obj1] => ftelement Object
    (
    [obj2] => ftelement Object
    (
    [property] => valore

    )

    )

    )

    Ho scritto la classe ma quello che mi viene fuori stampando l'istanza è questo:

    MyClass Object
    (
    [obj1] => MyElment Object
    (
    [obj2] => MyElment Object
    (
    [property] => MyElment Object
    (
    )

    )

    )

    )

  2. #2
    Utente di HTML.it
    Registrato dal
    Sep 2016
    Messaggi
    783
    Non ho capito bene la domanda, comunque puoi assegnare un oggetto ad una proprietà di una classe come se fosse una qualsiasi variabile. Poi in base alla visibilità potrai accedervi o meno dall'esterno della classe.

  3. #3
    In effetti non mi sono spiegato bene, ma sono riuscito nell'intendo, ho snellito la classe realizzata, per farti vedere il codice:

    Codice PHP:
    <?php
    class  tmpelement{
      private 
    $dato;
      function 
    __construct($value='') {
        if(
    is_string($value) || is_numeric($value)){
          
    $this->dato=$value;
        }
      }

      function 
    __set($name$value) {
        if(isset(
    $this->$name) && $this->$name instanceof tmpelement)
          return 
    $this->$name;
        if(
    property_exists('tmpelement',$name))
          return;
        
    $this->$name=new  tmpelement($value);
        return 
    $this->$name;
      }

      function 
    __get($name) {
        if(isset(
    $this->$name) && $this->$name instanceof tmpelement)
          return 
    $this->$name;
        if(
    property_exists('tmpelement',$name))
          return;
        
    $this->$name=new  tmpelement();
        return 
    $this->$name;
      }

      function 
    __toString() {
        if(
    is_numeric($this->dato))
          return (string)
    $this->dato;
        return (
    $this->dato) ? $this->dato '';
      }
    }

    class 
    tmp_header{
      private 
    $dato;
      function 
    __construct($value='') {
        if(
    is_string($value) || is_numeric($value)){
          
    $this->dato=$value;
        }
      }

      function 
    __set($name$value) {
        if(isset(
    $this->$name) && $this->$name instanceof tmpelement)
          return 
    $this->$name;
        if(
    property_exists('tmp_header',$name))
          return;
        
    $this->$name=new  tmpelement($value);
        return 
    $this->$name;
      }

      function 
    __get($name) {
        if(isset(
    $this->$name) && $this->$name instanceof tmpelement)
          return 
    $this->$name;
        if(
    property_exists('tmp_header',$name))
          return;
        
    $this->$name=new  tmpelement();
        return 
    $this->$name;
      }
      function 
    __toString() {
        if(
    is_numeric($this->dato))
          return (string)
    $this->dato;
        return (
    $this->dato) ? $this->dato '';
      }
    }

    class 
    tmp_master{
      public 
    $Header;
    }

    $xml=new tmp_master();
    $header=$xml->Header=new tmp_header();
    $header->Obj1->Obj2->str1='PIPPO';
    $header->Obj1->str1='PLUTO';
    $header->Obj1->Obj2->str2='PAPERINO';

    echo 
    '<pre>';
    echo 
    $header->Obj1->Obj2->str1.'<br>';
    echo 
    $header->Obj1->str1.'<br>';
    echo 
    $header->Obj1->Obj2->str2.'<br>';
    var_dump($xml);

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.