Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

$this

Status
Not open for further replies.

wolf73

Programmer
Feb 12, 2006
93
CA
I am trying to learn OO PHP. I get really confused looking at the code. I don't understand what detremine that we will use $this or not.
Like in the codde below.





if(($check_stage<=$called_stage)&&(!$errors_detected))
{
$this->model[$check_stage]->check_required_fields();

/**
* Check if any errors are loaded into the rf_error_codes array
*/
if(count($this->model[$check_stage]->rf_error_codes))
{
$this->errors = array_merge($this->errors,$this->model[$check_stage]->rf_error_codes);
/**
* Validation errors were detected for this model, we want to send the user to this
* step.
*/
$this->stage = $check_stage;

/**
* Set the errors detected flag, so we won't save the data or increment the stage
* counter. However, we want to continue looping and and building data for each model,
* so that to the user it will appear we're saving their data. When they return to the page
* the fields will be pre-filled with anything they've filled out so far.
*/
$errors_detected = true;
}


why not use $this in first if statment , like below.


if(($this->check_stage<=$called_stage)&&(!$errors_detected))

on what bases they decided not to us $this.


same way what made them decide use
$this->stage = $check_stage; not $stage = $check_stage;

and use
$errors_detected = true; NOT $this->errors_detected = true;
 
Let's use a simple class:

Code:
class Fi
{
	var $fy;

	function fo ($fum)
	{
  		$fubar = 3;
		if ($this->fy == $fubar + $fum)
		{
			print "yahoo";
		}
	}
}

The interesting bit is the line:

[tt]if ($this->fy == $fubar + $fum)[/tt]

which compares the class-level variable $fy (defined in the "var $fy" line and referenced as $this->fy) to the sum of the method-level variables $fubar and $fum (defined by explicit assignment or as the input parameter of the method, respectively).

Your script excerpt doesn't specify at what level the variables in question were declared, so I can only guess that in this line:

[tt]if(($check_stage<=$called_stage)&&(!$errors_detected))[/tt]

the comparison is between variables that are defined at the method-level.



Want the best answers? Ask the best questions! TANSTAAFL!
 
SO are you saying when a class level variables referenced in a function it should be refernced as $this,
what will happen if in your exapmple if you do it as
($fy == $fubar + $fum)
 
I assume you mean within the conditional in the method:

[tt]if ($fy == $fubar + $fum)[/tt]

so that the class definition would look like:

Code:
class Fi
{
    var $fy;

    function fo ($fum)
    {
          $fubar = 3;
        if ($fy == $fubar + $fum)
        {
            print "yahoo";
        }
    }
}

Since [tt]$fy[/tt] inside the method must reference a method-level variable (there's no "$this->") and since that variable does not exist, PHP will throw a Notice about the use of a non-existent variable used in a condition. Something like:

Notice: Undefined variable: fy in /path/to/script/script.php on line <line number>

Script execution will continue, though.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Thanks I got it, now following statment is with in a method.
Now they are creating an object


$this->form_field = new FormField();

What will be the reaon for doing it this way why not
$form_field = new FormField();
 
I don't know. Maybe the authors of the code wanted to initialize a class-level variable?


Do you understand the difference in length of existence between class-level variables and method-level variables?

Take the following script:

Code:
<?php
class A
{
    var $a;

    function set_function_level_a ($b)
    {
    	$a = $b;
    }
    
    function set_class_level_a ($b)
    {
    	$this->a = $b;
    }
    
    function show_all_a ()
    {
    	print '$a:' . $a;
    	print '<br>';
    	print '$this->a:' . $this->a;
    }
}

$a = new Fi();
$a->set_function_level_a(1);
$a->set_class_level_a(2);

show_all_a();
?>

The output of the script is:

[tt]Notice: Undefined variable: a in /home/sites/test/html/test_bar.php on line 18
$a:
$this->a:2[/tt]

The script generates the Notice in show_all_a() because the variable does not exist. $this->a exists, because class-level variables exist for the length of the existence of the object. $a does not, because method-level variables only exist for the length of the run of their respective methods.

$a exists and is assigned a value in set_function_level_a(), but ceases to exist as soon as the method quits running. When show_all_a() attempts to reference $a, it correctly produces the error because the method-level $a does not exist -- there has been no statement within show_all_a() which would make $a exist.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Thanks, Got it, now instead of declaring the variable at the top in the class will that be acceptable (just doing it straight in the method)
<?php
class A
{
function set_function_level_a ($b)
{
$a = $b;
}

function set_class_level_a ($b)
{
$this->a = $b;
}

function show_all_a ()
{
print '$a:' . $a;
print '<br>';
print '$this->a:' . $this->a;
}
}

$a = new Fi();
$a->set_function_level_a(1);
$a->set_class_level_a(2);

show_all_a();
?>
 
It will work, I think. But it's piss-poor programming style. And I have no idea how your idea will work once you add PHP's class variable visibility modifiers (public, private, protected) to the mix.

I strongly recommend that you declare class-level variables at the top of the class definition.




Want the best answers? Ask the best questions! TANSTAAFL!
 
i see this code

class YespaydayController extends BasicSiteController
{

protected function process_step1()
{

/**
* Creates a PostedLead object with most fields pre-filled
*/
$this->postedlead = $this->get_posted_lead();


}


}

here get_posted_lead(); is function in the class BasicSiteController, I was wondering why we calling the function like this why not

$this->postedlead = get_posted_lead(); ?


 
Because that's how you call a class method.

If you did:

$this->postedlead = get_posted_lead();

you'd be calling a function that is not a class method.

Run this test script:

Code:
<?php
function bar ()
{
	print "function bar\r\n";
}

class foo
{
	function bar ()
	{
		print "method bar\r\n";
	}
	
	function baz()
	{
		print "method baz\r\n";
		
		bar();
		
		$this->bar();
	}
}

$a = new foo();

print '<html><body><pre>';
$a->baz();
print '</pre></body></html>';
?>

It should output:

method baz
function bar
method bar





Want the best answers? Ask the best questions! TANSTAAFL!
 
i have a question about the following .... here in the line last line , are we creating an object from a class PostedLead and passing a variable to the constructor and then returning that object, right?



protected function get_posted_lead()
{
/**
* Set up PostedLead, with data from the current lead
*/

$attributes = Array(
"session_id" => $this->lead->session_id,
"cluster_id" => $this->lead->cluster_id,
"offer_id" => $this->lead->offer_id,
"site_id" => $this->lead->site_id,
"affiliate_id" => $this->lead->affiliate_id,
"sub" => $this->lead->sub,
"sub2" => $this->lead->sub2,
"email" => $this->lead->email,
"phone" => $this->lead->phone
);

return new PostedLead($attributes);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top