prezident Posted December 25, 2010 Share Posted December 25, 2010 How can i get my class to be showed on the front page here is the front page <?php require('ex2.php'); $start = new A(); $tart->Display(); ?> now here is ex2.php <?php ini_set('display_errors', 1); error_reporting(E_ALL); class A { public $title = "test1"; public $end = "test2"; } function __set($name, $value) { $this->$name = $value; } function Display() { echo $title; echo $end; } ?> shoudn't this print test1 and test2 ? Quote Link to comment https://forums.phpfreaks.com/topic/222638-classes/ Share on other sites More sharing options...
BlueSkyIS Posted December 25, 2010 Share Posted December 25, 2010 probably not, given this code: $start = new A(); $tart->Display(); Quote Link to comment https://forums.phpfreaks.com/topic/222638-classes/#findComment-1151366 Share on other sites More sharing options...
prezident Posted December 25, 2010 Author Share Posted December 25, 2010 probably not, given this code: $start = new A(); $tart->Display(); yea that was a typo i could rechange the post though but i changed it to $start and i get a fatel error it seems like it's not picking up the required file ... Fatal error: Call to undefined method A::Display() Quote Link to comment https://forums.phpfreaks.com/topic/222638-classes/#findComment-1151367 Share on other sites More sharing options...
the182guy Posted December 25, 2010 Share Posted December 25, 2010 And it should be echo $this->title; Quote Link to comment https://forums.phpfreaks.com/topic/222638-classes/#findComment-1151368 Share on other sites More sharing options...
BlueSkyIS Posted December 25, 2010 Share Posted December 25, 2010 ... and class functions must be included in the class definition: class A { public $title = "test1"; public $end = "test2"; function __set($name, $value) { $this->$name = $value; } function Display() { echo $this->$title; echo $this->$end; } } Quote Link to comment https://forums.phpfreaks.com/topic/222638-classes/#findComment-1151370 Share on other sites More sharing options...
prezident Posted December 25, 2010 Author Share Posted December 25, 2010 ... and class functions must be included in the class definition: class A { public $title = "test1"; public $end = "test2"; function __set($name, $value) { $this->$name = $value; } function Display() { echo $this->$title; echo $this->$end; } } thanx for the replies, but it's still giving me the fetal error from the main file... Quote Link to comment https://forums.phpfreaks.com/topic/222638-classes/#findComment-1151372 Share on other sites More sharing options...
BlueSkyIS Posted December 25, 2010 Share Posted December 25, 2010 and the error is.... ??? did you change this code yet? $start = new A(); $tart->Display(); Quote Link to comment https://forums.phpfreaks.com/topic/222638-classes/#findComment-1151373 Share on other sites More sharing options...
prezident Posted December 25, 2010 Author Share Posted December 25, 2010 and the error is.... ??? did you change this code yet? $start = new A(); $tart->Display(); yes i changed that error already, Fatal error: Call to undefined method A::Display() in /var/www/ex2-index.php on line 4 <?php require('ex2.php'); $start = new A(); $start->Display(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/222638-classes/#findComment-1151375 Share on other sites More sharing options...
.josh Posted December 25, 2010 Share Posted December 25, 2010 so ex2.php is in the same directory as that script that is running that code? Quote Link to comment https://forums.phpfreaks.com/topic/222638-classes/#findComment-1151378 Share on other sites More sharing options...
BlueSkyIS Posted December 25, 2010 Share Posted December 25, 2010 also, can we see the latest version of ex2.php? Quote Link to comment https://forums.phpfreaks.com/topic/222638-classes/#findComment-1151379 Share on other sites More sharing options...
prezident Posted December 25, 2010 Author Share Posted December 25, 2010 yeaup their both php files are in the same dir.. here is ex2.php <?php ini_set('display_errors', 1); error_reporting(E_ALL); class A { public $title="test1"; public $end="test2"; } function __set($name, $value) { $this->$name = $value; } function Display() { echo $this->$title; echo $this->$end; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/222638-classes/#findComment-1151380 Share on other sites More sharing options...
BlueSkyIS Posted December 25, 2010 Share Posted December 25, 2010 you must include your class functions within the class definition. see my comment above. class A { var $title = "test1"; var $end = "test2"; function __set($name, $value) { $this->$name = $value; } function Display() { echo $this->title; echo $this->end; } } full working example: <?php class A { var $title = "test1"; var $end = "test2"; function __set($name, $value) { $this->$name = $value; } function Display() { echo $this->title; echo $this->end; } } $start = new A(); $start->Display(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/222638-classes/#findComment-1151381 Share on other sites More sharing options...
prezident Posted December 25, 2010 Author Share Posted December 25, 2010 i did that earlier before you told me and it didn't work,(maybe i didn't save it smh) but now I'm getting Notice: Undefined variable: title in /var/www/ex2.php on line 17 Fatal error: Cannot access empty property in /var/www/ex2.php on line 17 i also made the var changes ** when your setting class attributes it's already set to public by default correct ? Quote Link to comment https://forums.phpfreaks.com/topic/222638-classes/#findComment-1151383 Share on other sites More sharing options...
.josh Posted December 25, 2010 Share Posted December 25, 2010 okay that latest error is because you should be using $this->title not $this->$title (same with $end and $name) Quote Link to comment https://forums.phpfreaks.com/topic/222638-classes/#findComment-1151386 Share on other sites More sharing options...
prezident Posted December 25, 2010 Author Share Posted December 25, 2010 thanx everybody finally working, that was a little code from a big code i created now I'm going to work out the big code ... thanx Quote Link to comment https://forums.phpfreaks.com/topic/222638-classes/#findComment-1151387 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.