Jump to content

PHP Class function problem


aeonsky

Recommended Posts

class Viewer {

public function get_dim_img($full_url) {

	$size = getimagesize('./'.$_SESSION['manga_directory'].'/'.$full_url);
	return $size;

}

public function print_image_with_black_div($picture) {

	$dim = $this->get_dim_img($picture); //LINE 16
	$width = $dim[0];
	$height = $dim[1];

	print "\n\n<div style=\"width: {$width}px; height: {$height}px\" class=\"black_b\">";		
	echo "<img src=\"index.php?p=$picture\" border=\"0\">";		
	echo '</div>';

}

}

 

This works...

Viewer::get_dim_img($picture);

 

But this doesn't...

$this->get_dim_img($picture);

 

I get this error...

Fatal error: Call to undefined method Content::get_dim_img() in /home/sorascan/public_html/smv/code/classes/Viewer.php on line 16

 

The only place where I call print_image_with_black_div is in viewer.php (different file, not Viewer.php)...

if($page) Viewer::print_image_with_black_div("$manga/$chapter/$page"); 
else Viewer::print_default_text();

 

I am very frustrated and do not know why $this doesn't work when calling another function within the same class.

 

Any help would be appreciated, thank you!

Link to comment
https://forums.phpfreaks.com/topic/155785-php-class-function-problem/
Share on other sites

Do you have two classes with the same method "get_dim_img" as it seems to think that you want to call Class "Content"'s function instead of Viewer.

 

I have the strangest feeling you omitted an important part of the code.

 

Aside from that, what is wrong with using Viewer:: ???

I have this in Content.php (Content class)...

	public function get_adj_menu_width($full_url) {

	$dim = Viewer::get_dim_img($full_url);
	$adj_width = $dim[0] + 2;

	return $adj_width;

}

 

get_dim_img() exists only in one class - Viewer class.

 

I call get_adj_menu_width() in the same Content class...

	public function print_menu_header($manga, $chapter, $page, $menu_array) {

	if(!$page) 	echo '<div class="usual" style="width: '.$_SESSION['default_width'].'px"><ul class="idTabs snow">';

	else {

		$adj_width = $this->get_adj_menu_width("$manga/$chapter/$page");
		if($adj_width < 600) $adj_width = "600";
		echo '<div class="usual" style="width: '.$adj_width.'px"><ul class="idTabs snow">';
																	  
	}

 

It is not terribly important to me, but I've always used $this-> when I'm in the same class. I guess just a matter of being consistent.

 

By the way, thanks for looking into this problem.

 

P.S. -> I'm thinking its a PHP bug because I read of stories that when people used a lot of function calls in function calls in another classes (I know it sounds random, and it is) they get the same kind of error that I did. And I went through the code 50 times, I do not believe that any of it is wrong.

P.S. -> I'm thinking its a PHP bug because I read of stories that when people used a lot of function calls in function calls in another classes (I know it sounds random, and it is) they get the same kind of error that I did. And I went through the code 50 times, I do not believe that any of it is wrong.

 

Hmm, I may create some tests just for fun.

 

But if you want to find if it is a bug, create a simple test. A class without all the bells and whistles and see if it is repeatable. If it is, then it may be a "bug" or may have been coded intentionally. But, if it is not repeatable with a simple class, then there is something going on with your code.

I just made one...

 

index.php

<?PHP

include("ClassOne.php");
ClassOne::function_two_in_Class_One("test");

?>

 

ClassOne.php

<?PHP

class ClassOne {

public function function_one_in_Class_One($lol) {

	print $lol;

}

public function function_two_in_Class_One($hmm) {

	$this->function_one_in_Class_One($hmm);

}


}

?>

 

It everything correct? I think it is, but it does not work.

 

This works...

ClassOne::function_two_in_Class_One($hmm);

 

Gives me this error...

Fatal error: Using $this when not in object context in C:\Users\Skynorth\Desktop\Localhost\bug\ClassOne.php on line 13

 

What do you think?

 

P.S. - I think I got what the problem is. I think once you use a Paamayim Nekudotayim, it treats the related function as if it was outside the class and thus, all the function inside the class are not accessible anymore. I read the page on www.php.net on Paamayim Nekudotayim, but it doesn't not say anything about this issue.

That is not a bug. You never instantiated an object by calling a method using the :: operator. This is how you should do it.

 

<?php

include("ClassOne.php");
$classOne = new ClassOne();

$classOne->function_two_in_Class_One("test");

?>

 

Is how you should use that function.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.