Jump to content

Issue with using a variable in another method


HDFilmMaker2112

Recommended Posts

For some reason in my getTitle function below it won't pull in the full_name from the getName function. The getName function is working perfectly fine; so the issue is somewhere in the getTitle function, I believe. "Page Not Found" prints out perfectly fine, when $this->invalid!="invalid" is false, When It's true I just get - in the browser title bar. That hyphen is only displayed when $section is found to be set.

 

<?php
public function getName(){
	if(isset($this->rows['middle_name']) && $this->rows['middle_name']!=null){
	$middle_name=" $this->rows['middle_name'] ";
	}
	else{
	$middle_name=' ';
	}
$this->full_name=$this->rows['first_name'].''.$middle_name.''.$this->rows['last_name'];
return $this->full_name;
}

public function getDisplayName(){
$this->maiden_name=$this->rows['maiden_name'];
return $this->maiden_name;
}

public function getTitle(){
	if($this->invalid!="invalid"){
	$section=$this->full_name;
		if(isset($this->rows['maiden_name']) && $this->rows['maiden_name']!=null){
		$section.=' ('.$this->rows['maiden_name'].')';
		}
		else{
		$section.='';
		}
	}
	else{
	$section='Page Not Found';
	}
return $section;
}
?>

 

You can see what's going on here:

http://www.kynxin.com/andrewmccarrick2

 

If you mouse over the images it'll have my name in the title attribute (you'll get the little bubble on mouseover). Those title attributes are feed via the getName() function.

 

If I remove the else statement in the getTitle function it seems $section is no longer set, and I no longer see the hyphen in the title bar... so for some reason $section is being rewritten over each time I try to concatenate it.

Link to comment
Share on other sites

I've tried a few things; still not working, but I wanted to posted the updated code:

 

<?php

class MyStream{
public function __construct($mystream_url){
$db = new DBConnection;
	$MyStreamDB = $db->connect('mysqli', 'persist', 'db_name');
	$mystream_url=$MyStreamDB->mysqli_sanitize($mystream_url);
	$MyStreamResult = $MyStreamDB->query("SELECT `prof`.`user_id`, `prof`.`university`, `prof`.`cover_picture`, 
	`prof`.`profile_picture`, `prof`.`first_name`, `prof`.`last_name`, `prof`.`middle_name`, `prof`.`maiden_name`, `prof`.`active`, `prof`.`location`,
	`prof`.`hometown`, `prof`.`work`, `prof`.`high_school`, `prof`.`relationship` FROM `user_profile` as `prof` 
	JOIN `user_details` as `details` ON `details`.`user_id`=`prof`.`user_id` WHERE `details`.`url`='$mystream_url'");
	$this->rows = $MyStreamResult->fetch_assoc();
	$this->mystream_num_rows = $MyStreamResult->num_rows;
}

public function returnMyStream(){
	if($this->mystream_num_rows==1){
	foreach($this->rows as $key=>$val){
        $return[$key] = $val;
	}
	return $return;
	}
	else{
	$this->invalid="invalid";
	return $this->invalid;
	}
}

public function getName(){
	if(isset($this->rows['middle_name']) && $this->rows['middle_name']!=null){
	$middle_name=" ";
	$middle_name.=$this->rows['middle_name'];
	$middle_name.=" ";
	}
	else{
	$middle_name=' ';
	}
$this->full_name=$this->rows['first_name'];
$this->full_name.=$middle_name;
$this->full_name.=$this->rows['last_name'];
return $this->full_name;
}

public function getDisplayName(){
$this->maiden_name=$this->rows['maiden_name'];
return $this->maiden_name;
}

public function getTitle(){
	if($this->invalid!="invalid"){
	$section=$this->full_name;
		if(isset($this->rows['maiden_name']) && $this->rows['maiden_name']!=null){
		$section.=" ";
		$section.="(";
		$section.=$this->rows['maiden_name'];
		$section.=')';
		}
		else{
		$section.='';
		}
	}
	else{
	$section='Page Not Found';
	}
return $section;
}
}

?>

Link to comment
Share on other sites

I have to call the method into the other ones? I thought I could just pass variables around using $this variable.

 

That did make it work, but I don't understand as to why. I'm using $this->invalid without calling in the returnMyStream method, and it works without an issue. So why am I having to call in getName but not returnMyStream?

Link to comment
Share on other sites

$this->full_name is defined within getName(). It therefore wont exist until getName() is called.

 

Your code is really quite all over the place and your using a lot of variables that aren't declared. You really should be defining all your variables at the top of your class.

Link to comment
Share on other sites

@HDFilmMaker2112 - remember that there's a difference between a function definition and a function invocation.  Code like the following:

<?php
    function example() {
        // code
        // code
        // code
    }
?>

 

Only defines functionality.  It doesn't actually run until you tell it to:

<?php
    example();
?>

 

The same thing applies to classes and their methods.  Defining a method isn't the same as invoking it.  It's not an OO thing, but rather PHP 101.  If you're uncomfortable with how functions and scope work, you really shouldn't be trying OO at all.

Link to comment
Share on other sites

@HDFilmMaker2112 - remember that there's a difference between a function definition and a function invocation.  Code like the following:

<?php
    function example() {
        // code
        // code
        // code
    }
?>

 

Only defines functionality.  It doesn't actually run until you tell it to:

<?php
    example();
?>

 

The same thing applies to classes and their methods.  Defining a method isn't the same as invoking it.  It's not an OO thing, but rather PHP 101.  If you're uncomfortable with how functions and scope work, you really shouldn't be trying OO at all.

 

Yeah I know how functions work, I've been coding in PHP for over 10 years and have been using functions for over 8; it's just every single example I've seen hasn't shown the need to call a method to pass variables from one to another.

 

The more and more I look into OOP, the more and more it seems like simple functions would work just as well. I'm getting the idea here, that all OOP is are functions wrapped in a class name; I really see nothing more to it than that. I frequently use functions that call other functions. I thought one of the benefits to OOP was the need not to do that; apparently not. With OOP you need to initiate a class via the "new" keyword and you get to use cool looking -> pointer characters to call functions... seems like there's nothing more than that compared to regular functions. Regular functions are just as good of a method to "re-use code", if not better it seems, than OOP.

 

And since I continually seem to be treated like I don't know anything about PHP on this forum, I won't be returning.

 

As per KillerPHP.com's OO beginner guide:

http://www.killerphp.com/tutorials/object-oriented-php/php-objects-page-3.php

 

As per one of their examples:

<?php           
        class person {          
        var $name;              
                public $height;         
                protected $social_insurance;
                private $pinn_number;
                
                function __construct($persons_name) {           
                        $this->name = $persons_name;            
                }               
                
                function set_name($new_name) {          
                        $this->name = $new_name;
                }       

                function get_name() {
                        return $this->name;
                }               
                        
        }
?>              

 

They're not calling the set_name function/method into the get_name function/method.

Link to comment
Share on other sites

The more and more I look into OOP, the more and more it seems like simple functions would work just as well. I'm getting the idea here, that all OOP is are functions wrapped in a class name; I really see nothing more to it than that.

 

This is generally how people that are yet to understand OOP see OOP. It is far from the reality of it all though. Having said that, OOP is not some magic bullet, and it is not always necessary.

 

And since I continually seem to be treated like I don't know anything about PHP on this forum, I won't be returning.

 

Sorry you feel that way but we can only judge you by the impression you make on us.

 

They're not calling the set_name function/method into the get_name function/method.

 

Have you actually tried instantiating that user class and calling get_name() ? What does it return? Nothing. Why, because you have to call set_name() first to give the $name property a value.

Link to comment
Share on other sites

They're not calling set_name() in the get_name() method because $this->name is initially set by the constructor.  In other words, by the time a Person object exists, $this->name will already have a value, which would be available for get_name() to use.

 

That tutorial is kinda shitty, by the way.  Hardly anyone uses the 'var' keyword in classes any more (it's a throwback to PHP 4, and best forgotten), and the 'var' keyword is not necessary to make a member public.  A data member without any access modifier will default to public, too.  The tutorial is also pretty poorly written.

 

The more and more I look into OOP, the more and more it seems like simple functions would work just as well. I'm getting the idea here, that all OOP is are functions wrapped in a class name; I really see nothing more to it than that. I frequently use functions that call other functions. I thought one of the benefits to OOP was the need not to do that; apparently not. With OOP you need to initiate a class via the "new" keyword and you get to use cool looking -> pointer characters to call functions... seems like there's nothing more than that compared to regular functions. Regular functions are just as good of a method to "re-use code", if not better it seems, than OOP.

 

That's because you've only looked at tutorial syntax.  OOP is not simply wrapping functions in a class and calling it a day.  If that's all it was, it wouldn't be used.  No, OOP is about creating small, discrete blocks of abstracted functionality that can be plugged into a variety of different systems in a variety of different ways.  Admittedly, functions have some of that, too (which makes sense, since OOP generally sprang up from procedural programming), but they don't address the entire picture.  Functions model a process - literally "Take these arguments and do something with them".  Objects represent things.  They are their own data types, and are used to build structures in code.

 

Online tutorials are really one of the worst ways to try to learn OOP.  They tend to begin and end with syntax, and never go into the why, which leads to the inevitable question "So... they just group functions together?"  Which is completely the wrong way to look at OOP.

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.