PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
Weasel5-12
Forum Commoner
Posts: 37 Joined: Tue Sep 16, 2008 6:58 am
Post
by Weasel5-12 » Mon Mar 30, 2009 1:14 am
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
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Mon Mar 30, 2009 1:20 am
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.
Weasel5-12
Forum Commoner
Posts: 37 Joined: Tue Sep 16, 2008 6:58 am
Post
by Weasel5-12 » Mon Mar 30, 2009 1:23 am
Cheers !
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
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Mon Mar 30, 2009 2:20 am
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.
Weasel5-12
Forum Commoner
Posts: 37 Joined: Tue Sep 16, 2008 6:58 am
Post
by Weasel5-12 » Mon Mar 30, 2009 2:30 am
Thankyou !
I had my c++ mixed up with my PHP =D
again, thankyou!
weasel