SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Thread: My first class

  1. #1
    Non-Member
    Join Date
    Oct 2008
    Posts
    372
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    My first class

    So I decided to start a little project to learn some OOP. I've started with a MySQL class. I'm wondering if I'm doing everything correct so far. I do have a few questions to ask though.

    PHP Code:
    <?php
    class MySQL
    {
        public function 
    __construct()
        {

        }

        public function 
    connect($host$user$pass)
        {
            return 
    mysql_connect($host$user$pass);
        }
        
        public function 
    query($query)
        {
            return 
    mysql_query($query);
        }
    }
    ?>
    As I understand, the __construct is suppose to tie everything together for you. But how is this done? As all functions(methods?) have different names. Is it always necessary to build a construct?

    In my connection, I use return mysql_connect. But I've seen some code as $this->mysql_connect($host, $user, $pass). Is there a difference?

  2. #2
    SitePoint Guru
    Join Date
    Oct 2006
    Location
    Queensland, Australia
    Posts
    852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sounds like you've still got a lot more learning to do. There are two many flaws in your knowledge for us to help you I'm sorry to say. Find a OOP tutorial (one for PHP 5). I don't have any recommendations myself, but maybe someone else does.

    I will tell you this though, constructor is not meant to tie everything together. It's meant to do whatever you want it to do. A constructor is not required and certainly not always needed, it's simply a function/method that is run when the class is instantiated into an object. It can do anything you want, whether it be run all the methods in your class, or simply set a variable.

  3. #3
    SitePoint Member
    Join Date
    Dec 2008
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    devzone"dot"zend"dot"com/node/view/id/627

    I would suggest going through the whole thing... but particularly around part 7 or 8.

    I have been in your shoes. I feel your pain. There's just sooooo much to explain - it's hard to know where to begin. I would strongly suggest going through that. I have been trying to learn PHP for years - and it's a tough road to hoe. Especially when I have ZERO background as a programmer.

    Keep your head up. Do a lot of reading. Then try to ask really specific questions.

    Hope this helps.


  4. #4
    SitePoint Member
    Join Date
    Dec 2008
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here are some other sites I have looked at in the past as well:

    www"dot"fluffycat"dot"com / PHP-Design-Patterns /

    www"dot"developer"dot"com / lang / php / article"dot"php / 3604111

    www"dot"killerphp"dot"com / videos / object-oriented-php-videos"dot"php

    www"dot"idea22"dot"com / video / category / PHP

    and of course (you'll have to pay) but there's always

    www"dot"lynda"dot"com

  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)
    I agree with the above comments 100&#37;. A class isn't a place to hold functions.

    A class holds properties (variables) and has methods (functions) which use those properties. So a better example of your code would be:
    PHP Code:
    <?php 
    class MySQL
    {
        private 
    $Connection;
        public 
    $Host$Username$Password;
        public function 
    __Construct($Host 'localhost'$Username 'root'$Password '')
        {
            
    $this->Connect($Host$Username$Password);
        }
        private function 
    Connect($Host$Username$Password)
        {
            
    $this->Host $Host;
            
    $this->User $Username;
            
    $this->Password $Password;
            
    $this->Connection mysql_connect($host$user$pass);
        }
        public function 
    Query($query)
        {
            return 
    mysql_query($query$this->Connection);
        }
    }
    Or, you could have another class for the query containing common query methods:
    PHP Code:
    <?php
    class MySQL
    {
        private 
    $Connection;
        public 
    $Host$Username$Password;
        public function 
    __Construct($Host 'localhost'$Username 'root'$Password '')
        {
            
    $this->Connect($Host$Username$Password);
        }
        private function 
    Connect($Host$Username$Password)
        {
            
    $this->Host $Host;
            
    $this->User $Username;
            
    $this->Password $Password;
            
    $this->Connection mysql_connect($host$user$pass);
        }
        public function 
    Query($query)
        {
            return new 
    Query($query$this->Connection);
        }
    }
    class 
    Query{
        public 
    $QueryString$Result$Error null;
        public function 
    __Construct($QueryString$Connection){
            
    $this->QueryString $QueryString;
            
    $this->Result mysql_query($QueryString$Connection);
            if(
    $this->Result === false){
                
    $this->Error mysql_error($Connection);
            }
        }
        public function 
    getResultArray(){
           
    Reset();
           
    $Return = array();
           while(
    $Row $this->fetchArray()){
               
    $Return[] = $Row;
           }
           
    Reset();
           return 
    $Return;
        }
        public function 
    fetchArray(){
            return 
    mysql_fetch_assoc($Result);
        }
        public function 
    Reset(){
            
    mysql_data_seek($this->Result0);
        }
    }
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  6. #6
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In my connection, I use return mysql_connect. But I've seen some code as $this->mysql_connect($host, $user, $pass). Is there a difference?
    $this is a reference to the object itself, it is only available inside class methods. Example:

    PHP Code:

    class Example
    {
        private 
    $string 'Hello World!';

        public function 
    hello()
        {
            echo 
    $this->string;
        }
    }

    $example = new Example();
    $example->hello(); 
    Which will print:

    Code:
    Hello World!

  7. #7
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    __construct() is a function that is called each time the class is initialized.

    PHP Code:
    class Example 

        private 
    $string;

        public function 
    __construct()
        {
            
    $this->string 'Hello World!';
        }

        public function 
    hello() 
        { 
            echo 
    $this->string
        } 


    $example = new Example(); 
    $example->hello(); 
    This will also print:

    Code:
    Hello World!

  8. #8
    Non-Member
    Join Date
    Oct 2008
    Posts
    372
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've heard that echo shouldn't be used and return is the preferred way yet I see it all over the place.

  9. #9
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, you have to be more specific. Echo shouldn't be used for what exactly?

    Echo and return have different meanings.

    You could as well do something like:

    PHP Code:
    class Example 

        private 
    $string null

        public function 
    __construct() 
        { 
            
    $this->string 'Hello World!'
        } 

        public function 
    getHello() 
        { 
            return 
    $this->string
        } 


    $example = new Example(); 
    $hello $example->getHello();
    echo 
    $hello

  10. #10
    Non-Member
    Join Date
    Oct 2008
    Posts
    372
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm sorry I ment within a method.

    PHP Code:

        
    public function hello()

        {

            echo 
    $this->string;

        } 
    I see all the time and hear that return should be used in methods and echos in the design part (outside the class)

  11. #11
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 9three View Post
    I'm sorry I ment within a method.

    PHP Code:

        
    public function hello()

        {

            echo 
    $this->string;

        } 
    I see all the time and hear that return should be used in methods and echos in the design part (outside the class)
    Yeah it depends on a class but usually it's a good advice.

  12. #12
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    It's normally best practice to have your class methods return data instead of echo it - simply because it is a lot harder to change what is output when your display logic is spread all over the classes. It's better to keep all your output to the presentation layer.


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
  •