Quick class question

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
aspekt9
Forum Commoner
Posts: 43
Joined: Wed Dec 06, 2006 5:03 pm

Quick class question

Post by aspekt9 »

Alright, I put all my news functions into a class and I just have a quick question. would I declare the variables I want to use in every function like this?:

Code: Select all

var $monthp = $_POST['theMonth'];
    var $day = $_POST['theDay'];
    var $year = $_POST['theYear'];
    var $hours = $_POST['theHour'];
Then reference them as $this->day, $this->year etc?

Because it returns an error:

Parse error: parse error, unexpected T_VARIABLE on each of those lines.
mentor
Forum Contributor
Posts: 100
Joined: Sun Mar 11, 2007 11:10 am
Location: Pakistan

Post by mentor »

you can't do like this. Try something like

Code: Select all

class test
{
	var $monthp = null; 
	var $day = null; 
	var $year = null; 
	var $hours = null;

	function test()
	{
		$this->monthp = $_POST['theMonth']; 
		$this->day = $_POST['theDay']; 
		$this->year = $_POST['theYear']; 
		$this->hours = $_POST['theHour'];
	}
}

$obj = new test();
echo $obj->monthp;
aspekt9
Forum Commoner
Posts: 43
Joined: Wed Dec 06, 2006 5:03 pm

Post by aspekt9 »

The only problem is that I want to use all those values in more than one function so I don't want to declare them each time I want to call a function. I want them to be kind of like global variables throughout each function.
mentor
Forum Contributor
Posts: 100
Joined: Sun Mar 11, 2007 11:10 am
Location: Pakistan

Post by mentor »

Do you know about constructor? take a look at this http://www.php.net/manual/en/language.o ... ructor.php
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

if you declare a local variable inside a method they're local only. To share them with the object (and other members) you have to use the $this->var syntax, and you should declare them at the beginning as mentor suggested.

It's ok to mix them:

Code: Select all

class test
{
        var $objectVar = null;

        function test()
        {
                $localVar = 'bob'; // isolated local variable
                $this->objectVar = 'frank'; // available to all methods as well as externally

                echo $localVar.' spanks '.$this->objectVar;
        }
} 

$t = new test;

$t->test(); // prints 'bob spanks frank'

echo $t->objectVar; // prints 'frank'
mentor
Forum Contributor
Posts: 100
Joined: Sun Mar 11, 2007 11:10 am
Location: Pakistan

Post by mentor »

Kieran, thank you for explaining :)
aspekt9
Forum Commoner
Posts: 43
Joined: Wed Dec 06, 2006 5:03 pm

Post by aspekt9 »

I've been doing that. I set all var = null; then give values to them in a function as $this->year = $_POST['year']; but when I go to use $this->year in another function it doesn't return anything.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

post your code
aspekt9
Forum Commoner
Posts: 43
Joined: Wed Dec 06, 2006 5:03 pm

Post by aspekt9 »

Actually I figured it out, the function name that stores the constructs had to be the same name as the class :) thanks for all the help guys.
Post Reply