SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 36
  1. #1
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help on understading behavior - stream resource is passed as an object. Why?

    Hello all,

    I have this singleton connection class to a socket:

    PHP Code:
    final public function __construct()
        {
            
    $this->_host 'ssl://example.hlp';
            
    $this->_porto 1231;

            try
            {
                
                
    $filePointer fsockopen($this->_host$this->_porto);

                
    //this will return a resource of stream type.
                //var_dump($filePointer);

                
    if ($filePointer === FALSE)
                {
                    throw new 
    Exception('error. OMG!!! OMG!!!!.');
                }
                else
                {
                    return 
    $filePointer;
                }

            }

            catch(
    Exception $e)
            {
                echo 
    "Erro de Conexão: " .$e->getMessage();
            }

        }

        public static function 
    getInstance()
        {
            if (
    self::$_instance === null)
            {
                
    self::$_instance = new self;
            }

            return 
    self::$_instance;
        } 
    I have an abstract class like so:

    PHP Code:
    public function __construct()
        {
           
            require_once(
    "Conexao.class.php"); 
            
    $this->_filePointer Conexao::getInstance();
           
            
    //this->_filePointer is an OBJECT not a resource stream anymore.
            //var_dump($this->_filePointer); 
           
        


    I know, even the method calls himself getINSTANCE so an object should be expected but I need a stream not an object, so that I can user $this->_filePointer AS A filePointer on other methods on this abstract class that will use fwrite for example.


    What can I do?

    Do I need to restructure this class relations somehow?
    Can I have any help please. Again.


    K. Regards,
    Márcio

  2. #2
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    If you only need the filePointer, try something like this:

    PHP Code:
    $this->connection Conexao::getInstance();
    $filePointer $this->connection->filePointer
    And in your Conexao class' __construct() do this:

    PHP Code:
    final public function __construct()
        {
            
    $this->_host 'ssl://example.hlp';
            
    $this->_porto 1231;
            
    $this->filePoint false;


            try
            {
                
    $this->filePointer fsockopen($this->_host$this->_porto);
                if (
    $this->filePointer === FALSE)
                {
                    throw new 
    Exception('error. OMG!!! OMG!!!!.');
                }
            }

            catch(
    Exception $e)
            {
                echo 
    "Erro de Conexão: " .$e->getMessage();
            }
        } 

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I'd be interested in seeing how you're planning to have these different objects interact, right now, it seems rather pointless having an object wrap just a handle.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  4. #4
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You rock.
    Do you care to explain what have we done here?
    And what have I was not doing there?



    I mean:
    PHP Code:
    $this->filePoint false
    Why doing it equal to false?


    Have we, from my first code to yours, passed the filepointer of our connection to a property, so that we can access it later by pointing to that property on the instance side?

    Can you please elaborate?

    trying to understand,
    Márcio

  5. #5
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    I'd be interesting in seeing how you're planning to have these different objects interact, right now, it seems rather pointless having an object wrap just a handle.
    That is a question that actually I would like to ask. But I didn't know how.

    But now that you've mention it... well... I don't really know what I'm doing... or... better, I'm not 100% sure about it.


    The point is to have a connection class, that connection class is doing the connection to the socket. OK.

    Then, the abstract class as some methods, it calls the connection on the constructor, then, it will have, as well, a sendFrame and getFrame methods.

    Those are methods, will be used by ALL methods on our commands class..
    Each command we do, sends and receives frames.

    And was those assumptions that make me thought on a structure like this.

    I know nothing about factories, interfaces, and so on... so... I was playing with the limited object oriented knowledge that I had.

    But, please, if it makes no sense, or you can thing of on a better way do deal with it, knowing that my OOP knowledge is not good.... please say so.

    Regards,
    Márcio

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Well, you'd be better suited having a Connection class that let's you interact with the pointer.

    You could then call Connection::write($data)/Connection::read($length) from another object which provides your frames etc...

    PHP Code:
    class Connection
    {
      protected
        
    $_pointer;
        
      public function 
    __construct($host$port){
        
    $this->_pointer fsockopen($host$port);
        if(
    false === is_resource($this->_pointer)){
          throw new 
    ZOMGException();
        }
      }
      
      public function 
    getPointer(){
        return 
    $this->_pointer;
      }
      
      public function 
    write($data){
        return 
    fwrite($this->_pointer$datastrlen($data));
      }
      
      public function 
    read($length 0){
        return 
    fread($this->_pointer$length);
      }
      
      public function 
    close(){
        return 
    fclose($this->_pointer);
      }

    PHP Code:
    class FrameManager
    {
      protected
        
    $_connection;
        
      public function 
    __construct(Connection $connection){
        
    $this->_connection $connection;
      }
      
      public function 
    writeFrame(){
        
    $this->_connection->write(/**/);
      }
      
      public function 
    readFrame(){
        
    $this->_connection->read(/**/);
      }

    Can you see how the Connection object bears no relevance to your application? It's too far down the food chain, which means you can use it in another application for say, talking HTTP.

    The next level up this 'food chain', we increase the knowledge the object knows about the application.

    This, is Abstraction - albeit poorly explained!
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
     throw new ZOMGException(); 
    Haha! That made my day


    @Oikram: I you want me to explain what I changed let me know, but Anthony's example is much more refined (and better) so I suggest you study his example

  8. #8
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    Well, you'd be better suited having a Connection class that let's you interact with the pointer.

    You could then call Connection::write($data)/Connection::read($length) from another object which provides your frames etc...

    PHP Code:
    class Connection
    {
      protected
        
    $_pointer;
        
      public function 
    __construct($host$port){
        
    $this->_pointer fsockopen($host$port);
        if(
    false === is_resource($this->_pointer)){
          throw new 
    ZOMGException();
        }
      }
      
      public function 
    getPointer(){
        return 
    $this->_pointer;
      }
      
      public function 
    write($data){
        return 
    fwrite($this->_pointer$datastrlen($data));
      }
      
      public function 
    read($length 0){
        return 
    fread($this->_pointer$length);
      }
      
      public function 
    close(){
        return 
    fclose($this->_pointer);
      }

    PHP Code:
    class FrameManager
    {
      protected
        
    $_connection;
        
      public function 
    __construct(Connection $connection){
        
    $this->_connection $connection;
      }
      
      public function 
    writeFrame(){
        
    $this->_connection->write(/**/);
      }
      
      public function 
    readFrame(){
        
    $this->_connection->read(/**/);
      }

    Can you see how the Connection object bears no relevance to your application? It's too far down the food chain, which means you can use it in another application for say, talking HTTP.

    I understand. Almost all.

    The issue that I'm not yet finding a way out of it, is that, THOSE fwrite and fread actually, on this case, seem that they cannot be so abstract. (maybe then can and I just can't figure them out how), but... let me give you an example:

    PHP Code:
    public function enviarFrame($xml//send frame that accepts an XML string
        
    {
            try
            {

                
    $bytesEscritos fwrite($this->_filePointerpack('N', (strlen($xml)+4)).$xml);

            ... 
    Here I'm using a fwrite yes, but inside, I'm using a pack with a given format (Big Endian on this case), the length is the length of my string yes... but I need to add those 4 bytes... at the beginning.

    The getFrame is not better either...

    PHP Code:
    //this will read the first 8x4 bits = 00000000 0000000 00000000 00000000 and store it in memory.
            
    $binaryStringPackOfFirstFourBytes fread($this->_filePointer4);

            
    //this will grab or binaryStringPack (formatted into Big Endian from the server), and unpack into an associative array.
            
    $unpacked unpack('Nlength'$binaryStringPackOfFirstFourBytes);

            
    //this will grab our lenght from our string array key.
            
    $length $unpacked['length'];

            
    //read the remaining of our stream: (minus the 4 bytes that were already readed on the first fread call.
            
    $remainBinaryPack fread($this->_filePointer, ($length 4)); 
    I will give a thought about how can all this (for me at least) complex relations can be split into different objects according to the structure suggested. But do you still believe the scheme provided by you will fit here?



    Thanks a lot,
    Márcio

  9. #9
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    class FrameManager
    {
        const 
    OFFSET 4;
        
        protected
            
    $_connection;
            
        public function 
    __construct(Connection $connection){
            
    $this->_connection $connection;
        }
        
        public function 
    sendFrame($frame){
            return 
    $this->_connection->write(
                
    pack('N'strlen($frame) + self::OFFSET) . $frame
            
    );
        }
        
        public function 
    getFrame(){
            
    $unpacked unpack('NLength'$this->_connection->read(self::OFFSET));
            return 
    $this->_connection->read(
                (int)
    $unpacked['length'] - self::OFFSET
            
    );
        }
    }

    class 
    Connection
    {
        protected
            
    $_resource;
            
        public function 
    __construct($hostname$port 1231){
            
    $this->_resource fsockopen($hostname$port);
        }
        
        public function 
    write($data$length null){
            return 
    fwrite(
                
    $this->_resource,
                
    $data,
                
    null === $length strlen($data) : $length
            
    );
        }
        
        public function 
    read($length){
            return 
    fread(
                
    $this->_resource,
                
    $length
            
    )
        }

    Will try to fill in the gaps later, after my morning coffee!
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  10. #10
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I will be shocked, then almost cry, and try studying it.

    Give me some hours, or an half day (i'm on the middle of other things as well, and some questions should arrive).


    Thank you very very much!
    Márcio
    Last edited by oikram; Jul 1, 2010 at 01:27. Reason: questions, not answers, will arrive... :s

  11. #11
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just a quick answer, if possible, no point on making a singleton connection class then?

    I thought it was a good think to guarantee only one instance of the connection object...


    Thanks in advance,
    Márcio

  12. #12
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Let's keep it simple for now.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  13. #13
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Lol. ok.

    But I had one already?!

    I will, try to grab the singleton structure from the above, and passed to this one.

    If I should not, because a lot of changes need to be made, let me know about it.
    And I will stop immediately.

    Here I goooo!!!
    Márcio

  14. #14
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You should be asking yourself, "Do I need it?" rather than, "Let me know if I shouldn't use it" .

    Leave. It. Alone.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  15. #15
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok. I will try to found when we need a singleton later on then.

    AHHHRRGG!!!!

  16. #16
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now with this structure where we need to instantiate the connection?

    I'm asking this because I'm getting an error...


    From the most abstract to the less abstract, so I hope:

    ConexaoEpp.Class.php
    PHP Code:
    class ConexaoEpp
    {

        
    //TO TEST: IF I CHANGE TO PRIVATE WHAT WILL HAPPEN ?
        
    protected $_filePointer//according to php documentation, fsocken returns a file pointer. I need to be strict to properly understand.



        
    public function  __construct($hostname='ssl://mysocketpath.com'$porto=1231)
        {
            
    $this->_filePointer fsockopen($hostname$porto);
        }

        public function 
    write($data$length null//by doing this, we are telling $data is mandatory and $length is optional.
        
    {
            
    //writes the contents of $data to the file stream pointed by our file pointer, until length is reached.
            //ternary operator: if $lenght is exactly equal to null, then, return the string length of $data, else, if (it's not NULL) return itself,
            //this means, return the value it already have;
            //WHY DO WE NEED THIS?
            
            //TO TEST: IF I CHANGE TO $length===null WHAT WILL HAPPEN?
            
    return fwrite($this->_filePointer$datanull===$length strlen($data) : $length);
        }

        public function 
    read($length)
        {
            return 
    fread($this->_filePointer$length);
        }



    EppFrameManager.Class
    Note that I needed to change the sendFrame attribute:
    PHP Code:
    class EppFrameManager
    {
        
    //const, because on all application, this value will never change. nice. :)
        
    const OFFSET 4;

        
    //TO TEST: WHY PROTECTED and not PRIVATE - because we will use $_conexao on a descendent class?
        
    protected $_conexao;

        public function  
    __construct(ConexaoEpp $conexao)
        {
            
    //pass the data received to a private property of our object
            
    $this->_conexao $conexao;
        }

        
    /**
         *
         * @param <string> $xml
         *
         * A frame, on this EPP context, is: the sum of a XML command instruction + 4 bytes containing the length.
         * It will be that group that we call frame.
         *
         * So, I believe, sendFrame will receive a $xml and send a frame by using fwrite.
         */
        
    public function sendFrame($xml)
        {
            
    //the (only?) way we have to access a const is by calling it staticly on self ? (no $this->OFFSET... ?)
            
    return $this->_conexao->write(pack('N'strlen($xml)+self::OFFSET).$xml);
        }

        public function 
    getFrame()
        {
            
    $unpacked unpack('NLength'$this->_conexao->read(self::OFFSET));

            
    //the reason why we are removing the FIRST (the length is at the beginning of our frame *) 4 bytes, is to be able to grab only the XML part of it,
            //so, instead of returned right away, the solution I found was, place the remaining binary pack into
            //a variable:

           //* before, on the "old" code, it was the fact that fread was called twice that guarantee to us, that we are accessing the FIRST 4 bytes. What about now? :s
            
    $binaryPack $this->_conexao->read((int)$unpacked['lenght'] - self::OFFSET);

            
    $xml = new SimpleXMLElement($binaryPack);

            return 
    $xml;
        }


    ComandosEppClass.php
    PHP Code:
    require_once ('EppFrameManager.Class.php');

    class 
    ComandosEpp extends EppFrameManager
    {

        
    /**
         *
         * @return <object>
         */
        
    public function hello()
        {

           
    //reads the XML send comands from the repository:
           
    $xmlObj simplexml_load_file('RepositorioXml/EppHello.xml');

           
    //pass then into a string so that it can be consumed by sendFrame;
           
    $xmlString $xmlObj->asXML();

           
    //sends the frame (conn->write->fwrite)
           
    $this->sendFrame($xmlString);

           
    //receives the frame containing the returned greeting comand:
           
    $greetingXML $this->getFrame();

           
    //return the greeting command on a simpleXMLElement object.
           
    return $greetingXML;

        }


    And finally:
    EppHello.php

    PHP Code:
    require_once('Classes/ComandosEpp.Class.php');

    $comandos = new ComandosEpp;

    $greeting $comandos->hello();

    $serverID $greeting->svID;

    echo 
    $serverID

    When I run it, I'm getting:
    Catchable fatal error: Argument 1 passed to EppFrameManager::__construct() must be an instance of ConexaoEppFccn, none given ...
    I would like to have it working, and then do some tests that I've marked and then, follow any suggestions you may have to make it better.

    About the error:
    Nothing as been passed to EppFrameManager construct.
    So, nothing as been passed on EppFrameManager instantiation...
    So, nothing as been passed on our Comandos class.
    And it's true, on Comandos (that extends EppFrameManager) nothing about connection is passed.

    I thought that THAT could be the issue, so I put it, on ComandosEppClass the following:

    PHP Code:
    public function  __construct(ConexaoEpp $conexao
    {
            
    parent::__construct($conexao);

    But I guess it wrong. Same error occours....
    We need to instantiate a connection object and pass it, I understand the error... but where?... I'm missing some dots...


    A little lost here... please have patience...
    M&#225;rcio
    Last edited by oikram; Jul 1, 2010 at 04:06. Reason: added a very important comment on getFrame

  17. #17
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Sorry M&#225;rcio, I'm currently sat in a meeting so don't have the time right now but here's a quick snippet that may help.

    You need to pass in the connection, will be back as soon as possible.
    PHP Code:
    <?php
    $connection 
    = new Connection();
    $command = new HelloCommand($connection);
    $response $command->execute();
    echo 
    $response->svID;
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  18. #18
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've added the following on commands construct...

    PHP Code:
    public function ComandosEpp()
        {
           
    $conn = new ConexaoEpp();

           return 
    $conn;
        } 
    And it seems to work...


    But then I need to include the conection on our comandosEPP and...
    that seems to miss the Abstraction propose described earlier right ?
    We are calling a far away (from the food chain) element into and element almost in front of that food chain...

    I've noticed you have replied, I will see the reply now. And see what it brings.

    From your example, we instantiate a connection object, then we passed to the hello commands.
    Since ALL comands will need a connection, probably passing it as a constructor of comandos class was not that bad idea? (probably it is... but I'm just asking. )

    I recall that we should use php __construct instead of a function with the same name as the class. I don't recall why. :S
    However, if I use __construct I will have an override. If I don't use __construct and I use a function the same name as the class, as I did, no override seems to occur.
    What's happening?

    Thanks in advance, "see" you later then,
    M&#225;rcio
    Last edited by oikram; Jul 1, 2010 at 04:48. Reason: added some infos AFTER reading the reply.

  19. #19
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $command = new HelloCommand(new EppConnection('example.org'1231));
    echo 
    $command->execute()->svID
    PHP Code:
    class Connection
    {
        protected
            
    $_pointer;
        
        public function 
    __construct($hostname$port){
            
    $this->_pointer fsockopen($hostname$port);
        }
        
        public function 
    write($data$length null){
            return 
    fwrite(
                
    $this->_resource,
                
    $data,
                
    null === $length strlen($data) : $length
            
    );
        }
        
        public function 
    read($length){
            return 
    fread(
                
    $this->_resource,
                
    $length
            
    )
        }
    }

    class 
    EppConnection extends Connection
    {
        const
            
    OFFSET 4;
        
        public function 
    sendFrame(SimpleXMLElement $frame){
            return 
    parent::write(
                
    pack('N'strlen($frame->asXML()) + self::OFFSET) . $frame->asXML()
            );
        }
        
        public function 
    getFrame(){
            
    $unpacked unpack('NLength'parent::read(self::OFFSET));
            return new 
    SimpleXMLElement(
                
    parent::read((int)$unpacked['length'] - self::OFFSET)
            );
        }
    }

    class 
    Command
    {
        protected
            
    $_connection,
            
    $_xml;
        
        public function 
    __construct(EppConnection $connection$file){
            list(
    $this->_connection$this->_xml) = array($connection, new SimpleXMLElement($file));
        }
        
        public function 
    execute(){
            
    $connection->sendFrame($this->_xml);
            return 
    $connection->getFrame();
        }
    }

    class 
    HelloCommand extends Command
    {
        public function 
    __construct(EppConnection $connection$file 'usr/anthony/web/epp/frames/hello.xml'){
            
    parent::__construct($connection$file);
        }

    I'm not entirely comfortable with it, but it took 5 minutes and I was in a meeting!
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  20. #20
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    [php]
    I'm not entirely comfortable with it, but it took 5 minutes and I was in a meeting!
    Thanks for making me feeling so ignorant.
    I will now read the reply.


    Thanks.

  21. #21
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    [php]
    $command = new HelloCommand(new EppConnection('example.org', 1231));
    echo $command->execute()->svID;

    Hello is one of many commands that we can request to the server.

    A workflow, of commands will be something like this:
    loginComand -> createContactComand -> createDomainComand -> logoutComand;
    This should not exceed the 30 seconds (or the server will shut down), but I guess that it will be ok.

    Each of those commands are, XML instructions, that we need to grab and send some node data before we actually execute the command.



    In your usage approach, however, it seems that, for each command we want to do, we need to establish a connection. But the intention was to allow us to make the connection, use as many commands as we need, and then close the connection again. Only one connection it's allowed.

    It was, probably, my bad English that lead us here. And I'm really sorry.


    Any thoughts? (after the meeting please, I'm feeling quite guilty already).


    Best regards,
    Márcio

  22. #22
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $connection = new EppConnection('example.org'1231);

    $command = new HelloCommand($connection);
    $command->execute();

    $command = new GetMeASandwichCommand($connection);
    $command->execute(); 
    Re-use the connection.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  23. #23
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    Re-use the connection.
    Ok. Object Oriented is really cool.

    I'm still screeching my head about what you said about abstraction and the fact that now, on each command, we need to use and reuse the $connectio

    I believe this is something to do with my lack of knowledge about singleton as well...

    Each time we reuse $connection we are not getting a new connection each time?


  24. #24
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by oikram View Post
    I'm still screeching my head about what you said about abstraction and the fact that now, on each command, we need to use and reuse the $connectio
    We've extended the base connection object to provide more application specific functions, we could of course create yet another object to pass in an instance of the base object if you wished.

    This seemed cleaner.

    Quote Originally Posted by oikram View Post
    Each time we reuse $connection we are not getting a new connection each time?
    Nope.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  25. #25
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok. That "nop" is here, and I will give another try.
    But before any advance, I must clarify a lot on your provided code.


    PHP Code:
    class Command
    {
        protected
            
    $_connection,
            
    $_xml;
        
        public function 
    __construct(EppConnection $connection$file){
            list(
    $this->_connection$this->_xml) = array($connection, new SimpleXMLElement($file));
        }
        
        public function 
    execute(){
            
    $connection->sendFrame($this->_xml);
            return 
    $connection->getFrame();
        }

    The method execute() will use $connection;
    Where does $connection come from?
    Shouldn't it be $this->_connection ? Or shouldn't it come from execute parameters or something?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •