any one can help me out of this simple php code ?

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

Post Reply
dreamnet
Forum Newbie
Posts: 9
Joined: Tue Mar 14, 2006 6:43 pm

any one can help me out of this simple php code ?

Post by dreamnet »

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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$this->standard;
dreamnet
Forum Newbie
Posts: 9
Joined: Tue Mar 14, 2006 6:43 pm

Post by dreamnet »

thanks, the result came out, 'doc1' which defined in initial var
but I called setType('strict'), my expectation should be 'doc2'
what happened?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

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 »

no work :(
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

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 »

done,thanks
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

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.
Post Reply