...

View Full Version : simple oop question



Anishgiri
12-21-2010, 08:37 AM
<?php

class test
{

function one()
{
$this->a=1;

}

function two()
{
$this->one();
echo $this->a;

}



}


$output=new test;
echo $output->two();

?>




Is there an alternative for $this->one() to call the one method, so I can get the variable $this->a?

Wizzard of Jazz
12-21-2010, 09:02 AM
Making two() as



function two()
{
echo $this->a;
}

will still make the var $this->a be echoed as long as it has been set somewhere else in the code.

I'm not entirely sure what exactly the problem is.

Wizzard of Jazz
12-21-2010, 09:03 AM
What about


class test
{

function __construct(){
$this->one();
}

function one()
{
$this->a=1;

}

function two()
{
echo $this->a;

}



}

poyzn
12-21-2010, 11:13 AM
class test
{
var $a = 1;

function two()
{
echo $this->a;
}
}

$test = new test();
$test->two();

Fou-Lu
12-21-2010, 01:42 PM
var usage is long deprecated.
PHP is a datatype weak language, so you do not want to allow an exposure of a variable using a class into a public scope ever. It also has a huge disadvantage in for whatever reason they have allowed anonymous writing of properties to classes which makes no sense whatsoever. We won't address this though.
Because of this datatype weakness, you must write mutators and accessors for your PHP to retain the integrity of the datatype you are working with. Your only options are to call functions, or take the risk that other developers will adhere to your datatype enforcement and expose public members (which I do not recommend, particularly for PHP).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum