TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Newbi OOP Question (http://www.talkphp.com/advanced-php-programming/1672-newbi-oop-question.html)

Gurnk 12-08-2007 02:53 PM

Newbi OOP Question
 
I am just starting to learn OOP, this very moment, and I have a question. I got a few things done (more than expected), but I am having some problems.

I am trying to just get a post from a database based on the ID.

PHP Code:

class post {
    var 
$id;
    
    function 
set_post($id) {  
        
$this->id $id
        
        
$post_query mysql_query("SELECT * FROM `v7_posts` WHERE id = '$this->id'");
        if(
mysql_num_rows($post_query) < 1) {
            return 
false;
        } 
        
        
$this->title mysql_result($post_query,'0',"title");
        
$this->post mysql_result($post_query,'0',"post");
        return 
true
    }

    function 
get_post() {
        return 
$this->title;
        return 
$this->post;
    }



Thats my class, and I'm using this to call it.

PHP Code:

$post = new post;
$post->set_post("1");
echo 
$post->get_post(); 

My problem is, that it is only returning the title, not the actual content. Am I doing this completely wrong?

Thanks! ^^

Rendair 12-08-2007 02:58 PM

maybe instead of echoing the $post->get_post();

You can do that within the class itself.

PHP Code:

function get_post() 
{
  echo 
$this->title."</br>";
  echo 
$this->post;


Because it might be overloading the return so its only returning the title.

Gurnk 12-08-2007 03:00 PM

Wouldn't I still have to echo the get_post function in order for anything to show up? And Isn't return doing the same thing as echoing?

EDIT: Got it :) I still echoed the get_post, but in the actual function, I used echo instead of return. :)

EDIT:
Is there an easier way to define the stuff, instead of this?

PHP Code:

$this->title mysql_result($post_query,'0',"title");
     
$this->post mysql_result($post_query,'0',"post"); 


Jay 12-08-2007 03:23 PM

Just for the record, when you return, you return.

The function returns the value and ends when you return, no if ands or buts! Therefor you can only return once in a function.

In PHP 5, I believe "var" is being deprecated, use public and private to declare variables:

PHP Code:

class MyClass
{
    public 
$foo 'bar';
    private 
$bar 'foo';


Secondly, take caution with echo and print, try not to use them like you did, one after another.. it's more logical to use it like such:

PHP Code:

function get_post()
{
    echo 
$var1 $var 2;
    
// personal I wouldn't use a function to return something that's public


Thirdly, you don't need a function to return variables (that is, if they're not private)

PHP Code:

    print $object->var1

Edit

Yes, there is an easier way, use arrays:

PHP Code:

class MyClass
{
    public 
$data = array( );

    public function 
data_to_array$result )
    {
        while( 
$data mysql_fetch_array$result ) )
        {
            
$this->data[] = $data;
        }
    }
}

print_r$object->data ); // look at how the array is structured

// when you figure the structure out, you can then access the array like:
print $object->data['foo']; 

^^

Gurnk 12-08-2007 03:30 PM

Wow! Thanks for all that useful information!

I knew about public and private, but I get an error when I try to use them. My server says its on PHP 5, but I guess its not... I'll have to ask my host about upgrading.

Thanks again. ^^

Jay 12-08-2007 03:35 PM

Your host probably runs PHP4 and PHP5 at the same time, yes it's possible, but sometimes annoying when working with PHP5, as PHP4 is dominant over the .php extension.

Try renaming your file to <name>.php5, and see if you still get the errors, if you don't, make a file over the FTP called ".htaccess", and add the following content into it:

Code:

        #        Modify the PHP Handler
        AddType x-mapp-php5 .php

Now you can work with PHP5 and the .php extension at the same time ^^

Gurnk 12-08-2007 04:02 PM

Thanks Jay! I got some code similar to that from my host, but it didn't work. But yours fixed the problem, thanks a lot. :)


All times are GMT. The time now is 05:27 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0