SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,563
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php 4 5 6, function and class, why class ?

    Well guys,

    I am doing php since I've joined this forum, started with kevin's book.

    Now, seeing that I'm still in php4 and most have already migrated to php5, I am considering to make the move as well.

    Problem is, all my sites I have done till now are in php4, and if I suddenly move to php5, what will I do with my sites done with php4 ... The idea to change the coding of all is scaring me a lot, as in my server I have php4 installed (php5 as cgi).

    Or, may be I should wait for php6 and then will go for two servers, one with php4 and the other with php6. In this regard, what you guys are doing. I would be interested to know this. How do you manage your sites that are done already in php4?

    Also, I was trying to understand the concept of class, thereby trying to make my first class ...

    Code PHP:
    class makeConnection { 
     
    	var $server;
    	var $login;
    	var $password;
     
        function makeConnection(){ 
        } 
     
        function connectToServer() {
    		$conn_id = ftp_connect($this->server);
    		 if (!$conn_id) {
    		 	return false;
    		 } else {
    		 	$login_result = ftp_login($conn_id, $this->login, $this->password); 
    			if (!$login_result) {
    				return false;
    			} else {
    				return true;
    			}
    		 }		
    	}
     
    }

    I don't know if this is the proper way to make class or there's a better way of making a class, but it seems to be working (I was pleasantly surprised to see this functionning, must admit)

    But the question I am asking to myself is, calling a class is long. Instead of calling this class as below, I could call that thro' a function with much less code.

    Code PHP:
    $connection=new makeConnection(); 
    $connection->server=$ftp_server; 
    $connection->login=$p_login; 
    $connection->password=$p_password; 
    $connection->connectToServer();
    vs.
    Code PHP:
    connection($ftp_server,$p_login,$p_password)

    What's your point guys? I am just trying to understand, having realized that I am left behind many in this regard ...

  2. #2
    SitePoint Enthusiast stoproductions's Avatar
    Join Date
    Feb 2007
    Location
    USA
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Get over to php5 fast. PHP themselves have announced they will be discontinuing support for php4. If you have straight functions i wouldn't worry to much about the change over. This may help: http://www.php.net/manual/en/migration5.php


    There are both pro's and con's to OOP. I suggest you go and do some research of your own. There have been enough debates over OOP in this forum alone to take up a few days of reading time.

  3. #3
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I always put as many details in the __construct (in PHP4, it's the same name as the class) function, as your second option.

    Also, I am a great user of chaining (which you can't do in PHP4). It's basically where every function returns the parent class, so you can carry on another function with that class, for example:
    PHP Code:
    $myclass->dosomething()->dosomethingelse()->display(); 
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  4. #4
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,563
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmmm stoproductions ... that means, if if I ask my host to throw away php4 and install php5 there, I shouldn't have too much problems ... but still, there are many 3rd party things (like swiftmailer) I am using who have seperate versions for php4 and php5, and for the moment I am using php4 ... will need several days or may be a month of work ...

    arkinstall, by "putting as many details in the construct", you mean the following is valid?

    Code PHP:
    class makeConnection { 
     
    	var $server;
    	var $login;
    	var $password;
     
        function makeConnection(){ 
    		$conn_id = ftp_connect($this->server);
        } 
     
        function connectToServer() {
    		 if (!$conn_id) {
    		 	return false;
    		 } else {
    		 	$login_result = ftp_login($conn_id, $this->login, $this->password); 
    			if (!$login_result) {
    				return false;
    			} else {
    				return true;
    			}
    		 }		
    	}
     
    	function disConnect() {
    		if($conn_id) {
    			ftp_close($conn_id);
    		}
    	}
     
    } 
     
     
    // then ...
     
    $connection=new makeConnection(); 
    $connection->server=$ftp_server; 
    $connection->login=$p_login; 
    $connection->password=$p_password; 
    $connection->connectToServer();
     
    // do stuff here
     
    $connection->disConnect();

  5. #5
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Quote Originally Posted by kigoobe View Post
    arkinstall, by "putting as many details in the construct", you mean the following is valid?
    I was thinking something more along the lines of this (in PHP5+):
    Code PHP:
    class makeConnection { 
    	var $server, $login, $password, $conn_id, $login_result;
        function __construct($server, $login, $password){ 
    		$this->server = $server;
    		$this->login = $login;
    		$this->password = $password;
    		if($this->conn_id = ftp_connect($this->server)){
    		 	$this->login_result = ftp_login($this->conn_id, $this->login, $this->password);
    		}else{
    			$this->disConnect;
    			return false;
    	}
     
    	function disConnect() {
    		if($conn_id) {
    			ftp_close($conn_id);
    		}
    	}
     
    } 
    //and use it like this:
    if(!$connection = new makeConnection($ftp_server, $p_login, $p_password)){
       die("ERROR!!!!");
    }
    //do some stuff....
    $connection->disConnect();
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  6. #6
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,563
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmmm ... this looks smarter, no doubt. And with much less code.

    And changing the name of the construct, it works even in php4. Great man !

    Time to learn OOPhp ... self talking ... he he he !!!

  7. #7
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,563
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Going thro' your code, I realized that we nowhere related $this->conn_id with $conn_id, and nowhere we used $login_result. Did we?

  8. #8
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    no, that was just a construction example
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  9. #9
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,563
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm ...

    So, this should rather be - or what?

    Code PHP:
    class makeConnection { 
        var $server, $login, $password, $conn_id;
        function makeConnection($server, $login, $password){ 
            $this->server = $server;
            $this->login = $login;
            $this->password = $password;
            if($conn_id=ftp_connect($this->server)){
                $this->login_result = ftp_login($conn_id, $this->login, $this->password);
            }else{
                $this->disConnect;
                return false;
    		}
        }
     
        function disConnect() {
            if($conn_id) {
                ftp_close($conn_id);
            }
        }
     
    }

    or may be -

    Code PHP:
    class makeConnection { 
        var $server, $login, $password, $conn_id;
        function makeConnection($server, $login, $password){ 
            $this->server = $server;
            $this->login = $login;
            $this->password = $password;
            if($conn_id=ftp_connect($this->server)){
                $this->login_result = ftp_login($conn_id, $this->login, $this->password);
            }else{
                //$this->disConnect; *** removing this as well ...
                return false;
            }
        }
     
        function disConnect() {
            if($conn_id) {
                ftp_close($conn_id);
            }
        }
     
    }

  10. #10
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The first one - It uses disconnect if the connection fails.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona


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
  •