Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Dec 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Beginner Help: Classes

    I'm just starting out to use PHP. I have version 4.4.4 that was preinstalled on my MacBook Pro. I want to know why this code does not produce what is expected:

    Code:
    <?php 
    
    class testClass {
    	$testvar;
    	$testvar2;
    	
    	function myFunction() {
    		echo $testvar."</br>".$testvar2."</br>";
    	}
    	
    	function setOne($str) {
    		$testvar = $str;
    	}
    	
    	function setTwo($str) {
    		$testvar2 = $str;
    	}
    }
    
    $mycls = new testClass;
    
    $mycls->setOne("hello");
    
    ?>
    At this point, Dreamweaver stops highlighting the syntax. This seems to happen right after the "->" operator. Then when I try to view the page in my browser, and all i get is:

    Code:
    setOne("hello"); ?>
    it seems that for some reason the "->" ends the php script. Is there something I'm not understanding about the way classes work? Any help is much appreciated. Thank you.

  • #2
    ess
    ess is offline
    Regular Coder
    Join Date
    Oct 2006
    Location
    United Kingdom
    Posts
    866
    Thanks
    7
    Thanked 30 Times in 29 Posts
    Here are a couple of things that I have noticed from your script.
    1- When you declare variables in a class, you should use the var keyword. This is only applicable to PHP4, in PHP5, you should state whether a variable is public, or private instead of using keyword var.
    2- When referring to a variable declared in a class, you should use $this->variable_name

    Here is a very simple class I have written to explain this further.

    Code:
    <?php 
    class Person {
    	var $name;  // instance variable to hold person's name
    	var $age;    // instance variable to hold person's age
    	
           // class constructor
    	function Person( $name, $age ) {
                    // when referring to instance variable $name, we use $this->name
    		$this->name = $name;
    		$this->age = $age;
    	} 
    	
    	function toString() {
    		return "<b>Name: </b>" . $this->name . "<br><b>Age: </b>" . $this->age;
    	}
    	
    } 
    // create a new instance of type Person
    $person	 = new Person( "John", "Smith" );
    // output person by calling toString function/method
    echo $person->toString();
    ?>
    if there is any other question, please let's know.

    good luck.

    Ess
    Last edited by ess; 12-24-2006 at 09:58 PM.

  • #3
    New to the CF scene
    Join Date
    Dec 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for your reply. I agree there were some errors in my code, and thank you for pointing them out.

    When I copy and paste this code into a new html file, and place it between the body tags, saving it as test.html, I still get an unexpected output:

    Code:
    name $this->name = $name; $this->age = $age; } function toString() { return "Name: " . $this->name . "
    Age: " . $this->age; } } // create a new instance of type Person $person = new Person( "John", "Smith" ); // output person by calling toString function/method echo $person->toString(); ?>
    I still see the problem with the "->" operator. It seems like its ending my php script. Is there something wrong with the way I put this script into an html page?

    Thanks.

  • #4
    New to the CF scene
    Join Date
    Dec 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So apparently if I save the file as a .php file it produces the right output.

    My question is can I not have any PHP script run in a file that isnt marked with the PHP extension? Is this something I must control using the server settings? If so, and I'm using apache, where would I find these settings in the httpd.conf?

  • #5
    New Coder
    Join Date
    Dec 2006
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think this should work:
    Code:
    AddType application/x-httpd-php4 .html
    It may be slightly different for you, so look for the AddType line in your httpd.conf file that allows files of type .php to run.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •