variables from outside class

Hi, ive got a few functions now for my media and felt it would be a good idea to put them in a class, but i cant seem to get my variables from outside…

here is my class:

[php]class media {
//START MEDIA
private $class_var;
//get media from media directory.
public function get_user_defined_media($media_cat, $display_recent) {
if (!empty($this->class_var = $media_id)) {
if ($this->class_var = $media_id == $media_cat) {
$display_recent = null;
}
}
$files = glob(MEDIA_BASEDIR.$media_cat.’/.txt’);
usort($files, function($x, $y) {
return $x < $y;
});
$recent_content = array_slice($files, 0, $display_recent);
foreach ($recent_content as $media) {
$line = file($media);
$media_title = ($this->class_var = $purifier->purify($line[0]));
$date_time = basename($media, ‘.txt’);
$content = file_get_contents($media);
$content = str_replace($media_title,’’,$content);
thememediatop($media_title, $media_cat, $this->class_var = $media_id);
print_r ($this->class_var = $purifier->purify($content));
thememediabottom($date_time);
}
}
//get media go back link when in view all.
public function media_goback_link () {
if (!empty($this->class_var = $media_id)) {
print (“

:leftwards_arrow_with_hook: Go Back To Media

\n”);
}
}
//require the html for media when no catagorie is selected.
public function get_media_page() {
if (empty($this->class_var = $media_id)) {
require_once PAGES_BASEDIR.‘Media/page.html’;
themeopendiv(‘Media Statistics’);
$this->media_stats(null);
themeclosediv();
}
}
//media stats block
public function media_stats($media_stats_display) {
$catagories = glob(MEDIA_BASEDIR.’
’, GLOB_ONLYDIR);
$catagories_slice = array_slice($catagories, 0, $media_stats_display);
$totalfiles = glob(MEDIA_BASEDIR.’/.txt’);
if ($totalfiles !== false) {
$totalfilecount = count($totalfiles);
} else {
$totalfilecount = 0;
}
if ($catagories !== false) {
$catcount = count($catagories);
} else {
$catcount = 0;
}
if ($media_stats_display == null) {
print (“

All Catagories.
\n”);
} else {
if ($catcount < $media_stats_display) {
$media_stats_display = $catcount;
}
print (“

Displaying {$media_stats_display} of {$catcount} Catagories.
\n”);
}
print (“There are {$totalfilecount} total posts.

\n”);
$catagories_slice = str_replace(MEDIA_BASEDIR,’’,$catagories_slice);
foreach ($catagories_slice as $catagorie) {
$files = glob(MEDIA_BASEDIR.$catagorie.’/.txt’);
if ($files !== false) {
$filecount = count($files);
} else {
$filecount = 0;
}
print ("{$filecount} in {$catagorie}
\n");
}
}
//media index page with pagination
public function media_index() {
$media_files = glob(MEDIA_BASEDIR.$this->class_var = $media_id.’/
.txt’);
usort($media_files, function($x, $y) {
return $x < $y;
});
$row_count = $this->class_var = $media_row_count;
$total_pages = ceil(count($media_files)/$row_count);
if (isset($_GET[‘p’]) && (is_numeric($_GET[‘p’]))) {
$p = htmlentities($_GET[‘p’], ENT_QUOTES | ENT_SUBSTITUTE, ‘UTF-8’);
if ($p > $total_pages) {
$p = 1;
}
} else {
$p = 1;
}
$offset = ($p-1)*$row_count;
$media_files = array_slice($media_files, $offset, $row_count);
foreach ($media_files as $media) {
$line = file($media);
$media_title = ($this->class_var = $purifier->purify($line[0]));
$date_time = basename($media, ‘.txt’);
$content = file_get_contents($media);
$content = str_replace($media_title,’’,$content);
thememediatop($media_title, $this->media_id, $this->media_id);
print_r ($this->class_var = $purifier->purify($content));
thememediabottom($date_time);
}
print ("

");
if ($total_pages > 1) {
print (“

Currently viewing page {$p} of {$total_pages}

\n”);
if ($p != 1) {
print (“⇽ Prev  \n”);
}
$media_pages = range(1, $total_pages);
foreach ($media_pages as $pages) {
if ($p != $pages) {
$link_name = $pages;
} else {
$link_name = ("{$pages}");
}
print (" {$link_name} \n");
}
if ($p != $total_pages) {
print ("  Next ⇾\n");
}
}
print ("
\n");
}
//END MEDIA
}[/php]

in my mainfile i add

[php]$media = new media($purifier, $media_id, $media_row_count);[/php]

then calling my functions:

[php]$media->media_index();//for media page

$media->get_user_defined_media(‘Announcements’, 1);//to place other pages

$media->media_stats(4);//getting stats for media - 4 is the number of catagories to show[/php]

i would like to get this working as it shrinks the number of variables i have to use
any help would be great,
thanks

What do you mean,

cant seem to get my variables from outside…

like normaly i would have done this for example…

[php]function media_index($purifier, $media_id, $media_row_count) {
}
media_index($purifier, $media_id, $media_row_count);
[/php]

but now i want to limit the number of variables i use and tidy up/group my functions so it becomes somthing like this…

[php]class media {

private $class_var;

public function media_index() {
$this->class_var = $media_id;
$this->class_var = $purifier;
$this->class_var = $media_row_count;
}
}
$media = new media($purifier, $media_id, $media_row_count);
$media->media_index();//for media page
$media->get_user_defined_media(‘Announcements’, 1);//to place other pages
$media->media_stats(4);//getting stats for media - 4 is the number of catagories to show[/php]

i get undefined variables on those three…

thanks

The class has no constructor, nor does have those instance variables.

Please can u give a example on how to do that?

Thanks

[php]

<?php abstract class Shape { protected abstract function area(); protected abstract function perimeter(); } class Square extends Shape { private $_width; private $_height; public function __construct($h, $w){ if ($h <= 0) throw new Exception('Height must be a positive number greater than 0.'); if ($w <= 0) throw new Exception('Width must be a positive number greater than 0.'); $this->_height = $h; $this->_width = $w; } public function area(){ return $this->_width * $this->_height; } public function perimeter(){ return ($this->_height * 2) + ($this->_width * 2); } } [/php]

is this ok…

[php]class media {
//START MEDIA

protected $purifier;
protected $media_id;

public function __construct ($purifier, $media_id) {
	$this->purifier = $purifier;
	$this->media_id = $media_id;
}

//get media from media directory.
public function get_user_defined_media($media_cat, $display_recent) {
	if (!empty($this->media_id)) {
		if ($this->media_id == $media_cat) {
			$display_recent = null;
		}
	}
	$files = glob(MEDIA_BASEDIR.$media_cat.'/*.txt');
	usort($files, function($x, $y) {
		return $x < $y;
		});
	$recent_content = array_slice($files, 0, $display_recent);
	foreach ($recent_content as $media) {
		$line = file($media);
		$media_title = ($this->purifier->purify($line[0]));
		$date_time = basename($media, '.txt');
		$content = file_get_contents($media);
		$content = str_replace($media_title,'',$content);
		thememediatop($media_title, $media_cat, $this->media_id);
		print_r ($this->purifier->purify($content));
		thememediabottom($date_time);
	}
}

}

$media = new media($purifier, $media_id);

$media->get_user_defined_media(‘Announcements’, 1);[/php]

thanks

A class defines an Object. So, you have a class person.

[php]
class Person {
private $height;
private $hair_color;
private $name;
private $eye_color;
}[/php]

That is what the properties [ instance variables ] are, they are variables that define properties of the class.

Class methods describe actions. What the object can do.

[php]class Person {
private $height;
private $hair_color;
private $name;
private $eye_color;

public function walk(){}

public function run(){}

public function sleep(){}

}[/php]

If you don’t need the variables to define the object, then they are wasted in the class. Likewise, if the method in the class doesn’t have anything to do with the SINGLE principle usage of that class, it doesn’t belong in that class.

so is it ok if i use it like this?

[php]class media {
//START MEDIA

private $purifier;
private $media_id;

public function __construct ($purifier, $media_id) {
	$this->purifier = $purifier;
	$this->media_id = $media_id;
}

}[/php]

thanks

That is semantically correct. Now define the methods that the media class should do.

Awesome, ive got all that working now :slight_smile:

i was wondering how i can extend the construct fucnction to be shared with other classes?

like

[php]class variables {
private $purifier;
private $media_id;
private $media_row_count;
public function __construct($purifier, $media_id, $media_row_count) {
$this->purifier = $purifier;
$this->media_id = $media_id;
$this->media_row_count = $media_row_count;
}
}
class classA extends variables {

public function a_function() {
	$this->media_id
}

}

class classB extends variables {

public function b_function() {
	$this->media_id
}

}[/php]

i tried this method in my code and it didont work, do they not become one when there extended?

thanks

Not how it works.

You extend your parents, but you are not your parents. So, while you have their traits, that doesn’t mean that you are them.

I think I understand I will look into that.

Would there be another way of doing it so I dont have to repeat my variables as much when I make more classes?

Thanks

You can create a class with those values. Then, pass the instance of that class around to the classes that depend on it.

Sponsor our Newsletter | Privacy Policy | Terms of Service