Jump to content

[SOLVED] Put Class Variable In New Non-Class Function?


Northern Flame

Recommended Posts

I have a variable that is generated in the functions in my class,

but for some reason, I can't pass that variable in my new function

that is made outside of the class. Here's pretty much what I'm doing:

 

<?php

include('./../_CLASSES/lyrics/view.php');

$echo = new lyrics;

if(!isset($_GET['lyrics'])){
	lyrics::search();
} else{
	lyrics::grabInfo(mysql_real_escape_string($_GET['lyrics']));
	lyrics::getThisEdit();
	lyrics::filterLyrics();
	lyrics::makeHTMLfriendly();
	lyrics::generateHTML();
}

function pageContent($html = $echo->html){
	echo $html;
}

include('./../templates/'. $config['template'] .'/template.php');

?>

 

I also tried:

 

<?php

include('./../_CLASSES/lyrics/view.php');

$echo = new lyrics;

if(!isset($_GET['lyrics'])){
	lyrics::search();
} else{
	lyrics::grabInfo(mysql_real_escape_string($_GET['lyrics']));
	lyrics::getThisEdit();
	lyrics::filterLyrics();
	lyrics::makeHTMLfriendly();
	lyrics::generateHTML();
}

function pageContent($html = $echo){
	echo $html->html;
}

include('./../templates/'. $config['template'] .'/template.php');

?>

 

 

**Difference:

1)

function pageContent($html = $echo->html){

echo $html;

}

2)

function pageContent($html = $echo){

echo $html->html;

}

 

 

heres the error I receive both times:

Parse error: parse error, unexpected T_VARIABLE in /path/to/website.com/lyrics/view.php on line 17

 

heres line 17:

function pageContent($html = $echo->html){

 

can anyone please help?

Link to comment
Share on other sites

Two things:

 

1. You're trying to set the default argument ( function pageContent($html=$echo...) ) by referencing to something out of the constant scope. In short, you can't do that. What you could do, if you want to keep with your design, is have it null by default, check in the function, and set it by checking if it's null. Example:

function pageContent($html='') {
if (!$html) {
 global $echo; //Include the global $echo in the scope - This is if the script is executed in the global level - VERY iffy in this context
 $html=$echo->html;
}
return $html;
}

To be honest, that design is a little odd, for lack of a better word. You could do something more like this (assuming that pageContent() is only called once, which it should be):

function pageContent() {
//Changing a few things - read the next point
$obj=new lyrics(); //Generic name - a little better than just $echo
if (!isset($_GET['lyrics'])) $obj->search(); //Not static
else {
 $obj->grabInfo(...);
 $obj->getThisEdit();
 //And so on...
}
$obj->generateHTML(); //Wouldn't you want this for either case?
return $obj->html;
}

 

2. Why do you use static methods (i.e. lyrics::search()) in this sense? They look like they use instance-level data... You might want to go over the OOP Section and look at methods once more.

Link to comment
Share on other sites

thanks ill give that a try

 

 

2. Why do you use static methods (i.e. lyrics::search()) in this sense? They look like they use instance-level data... You might want to go over the OOP Section and look at methods once more.

 

 

I didnt know it made a big difference. I thought lyrics::search() and $echo->search() did the same thing?

the reason I use this method, lyrics::search(), is because a lot of the times the variables inside my classes

are established and used inside the functions of the class and I never have to call them, so I hardly ever

do stuff like:

$echo = new lyrics;

 

the only reason i did it in this case is because I needed to echo out the html that is generated in

the functions.

 

but for future reference, is there something wrong with doing it that way?

Link to comment
Share on other sites

Those only do the same thing if they're called in the scope of the class, or if you simply don't make any references to $this (instance level members/methods).

Static scope, in a simple sort of way, means that the data is the same for all instances (instances are objects instantiated with new) - it's stored 'in the class' as opposed to in the instance

Say if you have two instance of lyrics, say 'bob' and 'george', and you only refer to static members with the methods, '$bob->func()' and '$george->func()' will do the same thing as 'lyrics::func()' (where 'func' is any function you have defined) - meaning you don't even need those two instances. This is a situation you would NOT want.

 

With simple OOP, you can organize and separate data a little better. Here's just an example:

class Person {
public $name, $age;
function __construct($name,$age) { //Instance level - this is the function called for new Person(...)
 $this->name=$name;
 $this->age=$age;
}
function func() { //This function is instance-level - References to $this means it gets data from the instances, so it can be different per instance
 echo "Person: $this->name - $this->age<br/>";
}
}
$bob=new Person('Bob',26);
$george=new Person('George',24);
$bob->func();
$george->func();

Output:

Person: Bob - 26
Person: Geroge - 24

 

Also, reading the manual will do a lot more for you than me just making up random examples.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.