Page 1 of 1

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

Posted: Tue Mar 14, 2006 6:45 pm
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();


?>

Posted: Tue Mar 14, 2006 6:51 pm
by feyd

Code: Select all

$this->standard;

Posted: Tue Mar 14, 2006 6:54 pm
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?

Posted: Tue Mar 14, 2006 6:54 pm
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)
?>

Posted: Tue Mar 14, 2006 6:59 pm
by dreamnet
no work :(

Posted: Tue Mar 14, 2006 7:29 pm
by feyd
all the references to $standard must be $this->standard except for the declaration.

Posted: Tue Mar 14, 2006 7:35 pm
by dreamnet
done,thanks

Posted: Tue Mar 14, 2006 9:17 pm
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.