Page 1 of 1

OOP with PHP.

Posted: Mon Mar 30, 2009 1:14 am
by Weasel5-12
G'day,

Though not new to php, i am exploring oop in php.
I've started to do some VERY basic testing with php classes, and have a return variable error.

<?php

class_lib.php

Code: Select all

class drink {
    //var $drinkNam;
    //var $ingredientList;
    var $drinkURL;
    //var $blank;
    
    function set_url($url){
        $drinkURL = $url;
    }
    
    function get_url(){
        return $this->drinkURL; 
    }
}
?>
 
index.php

Code: Select all

<html>
<head>
<title>Test Doc #1</title>
</head>
<?php include("class_lib.php"); ?>
 
<body>
<?php 
    $url = "Bloody Mary";
    
    $d1 = new drink();
    
    //$d1->set_url($url);
    $drink1URL= $d1->get_url();
    
    echo "Drink name: "+$drink1URL;
 
?>
</body>
</html>

Upon testing these, i get an output of 0. Is this because the return value only returns int variables or is it because of my concanination?

thankyou,
weasel

Re: OOP with PHP.

Posted: Mon Mar 30, 2009 1:20 am
by Chris Corbyn
In set_url() you're missing the $this.

I'm not 100% why you see a zero however. The variable has not been set so its value should be null, which PHP should cast as an empty string.

Either way, you're trying to output a variable (property actually) that has not been set.

Re: OOP with PHP.

Posted: Mon Mar 30, 2009 1:23 am
by Weasel5-12
Cheers ! :drunk:

I just caught that. Though, when attempting to echo out the URL with preceeding text, it still returns 0.

I think it is because of the concanination.


weasel

Re: OOP with PHP.

Posted: Mon Mar 30, 2009 2:20 am
by requinix
Weasel5-12 wrote:I think it is because of the concanination.
Correct. Kinda.

You're trying to do concatenation but you're doing it wrong. + is only for addition: as such, if PHP sees one it'll make sure that what's on the left and what's on the right are both numbers. If they are not, PHP automatically converts whatever they are into numbers.
Blah blah blah...

Concatenation is with the . operator.

Code: Select all

echo "Drink name: ".$drink1URL;

Re: OOP with PHP.

Posted: Mon Mar 30, 2009 2:30 am
by Weasel5-12
Thankyou !

I had my c++ mixed up with my PHP =D

again, thankyou!


weasel