Jump to content


$this-> ?


5 replies to this topic

#1 Mr. Jay

    Young Padawan

  • Members
  • Pip
  • 81 posts

Posted 13 May 2007 - 12:31 AM

I was wondering how to get $this->test->main() to work ;)

<?php
	class test
	{
		function main()
		{
			return rand(1,10000);
		}
	}
	echo $this->test->main();
?>

Thanks :(

#2 Hayden

    P2L Jedi

  • Members
  • PipPipPip
  • 716 posts
  • Gender:Male
  • Location:Texas

Posted 13 May 2007 - 01:15 AM

$this->main();
that is only used within the class as such
class test {
   function main() {
	  $hello = "Hello World";
	  return $hello;
   }
   echo $this->main();
}

if you want to use it outside of the class, for instance in your index.php where you have included the file where the class is located.

then you would do this...
<?php
include "file_with_class.php";

$temp = new test();

echo $temp->main();
?>


#3 Mr. Jay

    Young Padawan

  • Members
  • Pip
  • 81 posts

Posted 13 May 2007 - 01:35 AM

Thank you SpatialVisionary ;)

#4 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 13 May 2007 - 01:57 AM

Slight correct on the "used within a class" statement.

class test {
   function bar(){
	  return 'The bar() method.';
   }

   function main() {
	  echo $this->bar();
   }
}

$obj = new test();
$obj->main();
Last i checked, all that a class definition could contain was function definitions, not calls to functions.

More info can be found at php.net, nothing specifically about $this though (which is sort of depressing).
http://php.net/manua...anguage.oop.php
http://php.net/manua...ef.classobj.php

Edited by rc69, 13 May 2007 - 02:01 AM.


#5 Mr. Jay

    Young Padawan

  • Members
  • Pip
  • 81 posts

Posted 14 May 2007 - 02:31 AM

One more thing is it possible to do $temp->username->main('lalala')

username = $_GET['name'] or w/e

In return it'll show (username): lalala

<?php
	class test
	{
		function main($text)
		{
			return (username?).": ".$text;
		}
	}
?>

<?php
include "test.php";

$temp = new test();

echo $temp->username->main('lalala');
?>

Thanks :P

Edited by Mr. Jay, 14 May 2007 - 02:33 AM.


#6 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 14 May 2007 - 12:11 PM

For that it would probably be best to store the username as a variable, not as an object.
<?php
class test{
	var $username;
	function main($text){
		return $this->username.": ".$text;
	}
}
?>
But yes, you can do what you want, but i've never seen the need for objects within objects, so you'll have to figure out the class set up.
<?php
class test{
	function main($text){
		return $this->name.": ".$text;
	}
}

class temp{
	var $username;
	var $name;
	function temp($name){
		$this->name = $name;
		$username = new test();
	}
}

$temp = new temp('blah');
?>
Again, go to php.net if you have more questions. As it stands, i think you need a better understanding of OOP before messing around with it to much. Not that it is dangerous or anything, it is just that you don't always need to use OOP to accomplish what you want to do.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users