...

View Full Version : Beginner Help: Classes



aoberoi
12-24-2006, 10:03 PM
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:


<?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:


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.

ess
12-24-2006, 10:56 PM
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.


<?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

aoberoi
12-24-2006, 11:46 PM
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:


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.

aoberoi
12-25-2006, 01:31 AM
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?

Crimsonjade
12-25-2006, 01:55 AM
I think this should work:


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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum