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
dreamnet
Forum Newbie
Posts: 9 Joined: Tue Mar 14, 2006 6:43 pm
Post
by dreamnet » Tue Mar 14, 2006 6:45 pm
I created two files,and I dont know what happend, function getType return an empty value; any one can give me a help?
my_type.php
Code: Select all
<?php
class my_Type{
var $standard ='doc1';
function my_Type(){
$this->getType();
}
function getType(){
return $standard;
}
function setType($type_style){
switch (strtolower((string)$type_style)) {
case 'one':
$standard = 'doc1';
break;
case 'two':
$standard = 'doc2';
break;
default:
$standard = '';
break;
}
}
}
?>
-------------------------------------------
index.php
Code: Select all
<?php
require('my_type.php');
$web_type = new my_Type();
$web_type -> setType('strict');
echo $web_type -> getType();
?>
Last edited by
dreamnet on Tue Mar 14, 2006 9:02 pm, edited 1 time in total.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Mar 14, 2006 6:51 pm
dreamnet
Forum Newbie
Posts: 9 Joined: Tue Mar 14, 2006 6:43 pm
Post
by dreamnet » Tue Mar 14, 2006 6:54 pm
thanks, the result came out, 'doc1' which defined in initial var
but I called setType('strict'), my expectation should be 'doc2'
what happened?
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Tue Mar 14, 2006 6:54 pm
setType doesn't return anything. Have it return something.
Code: Select all
<?php
class my_Type{
var $standard ='doc1';
function my_Type(){
$this->getType();
}
function getType(){
return $standard;
}
function setType($type_style) {
switch (strtolower((string)$type_style)) {
case 'transitional':
$standard = 'doc1';
break;
case 'strict':
$standard = 'doc2';
break;
case 'frameset':
$standard = 'doc3';
break;
default:
$standard = '';
break;
}
return $standard;
}
}
?>
Then use it
Code: Select all
<?php
require('my_type.php');
$web_type = new my_Type(); // Returns 'doc1'
$web_type->setType('strict'); // Returns 'doc2'
echo $web_type->getType(); // returns 'doc2' (I think)
?>
dreamnet
Forum Newbie
Posts: 9 Joined: Tue Mar 14, 2006 6:43 pm
Post
by dreamnet » Tue Mar 14, 2006 6:59 pm
no work
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Mar 14, 2006 7:29 pm
all the references to $standard must be $this->standard except for the declaration.
dreamnet
Forum Newbie
Posts: 9 Joined: Tue Mar 14, 2006 6:43 pm
Post
by dreamnet » Tue Mar 14, 2006 7:35 pm
done,thanks
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Tue Mar 14, 2006 9:17 pm
feyd wrote: all the references to $standard must be $this->standard except for the declaration.
Man, I feel stupid. I looked right over that, AFTER you posted about it the first time. I even posted bad code back. Sorry all. Thanks again Feyd.