Jump to content

classes


prezident

Recommended Posts

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 ?

Link to comment
https://forums.phpfreaks.com/topic/222638-classes/
Share on other sites

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()

Link to comment
https://forums.phpfreaks.com/topic/222638-classes/#findComment-1151367
Share on other sites

... 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;
    }
}    

Link to comment
https://forums.phpfreaks.com/topic/222638-classes/#findComment-1151370
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/222638-classes/#findComment-1151372
Share on other sites

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();
?>

Link to comment
https://forums.phpfreaks.com/topic/222638-classes/#findComment-1151375
Share on other sites

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;
    }

?>

Link to comment
https://forums.phpfreaks.com/topic/222638-classes/#findComment-1151380
Share on other sites

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();
?>

Link to comment
https://forums.phpfreaks.com/topic/222638-classes/#findComment-1151381
Share on other sites

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 ?

Link to comment
https://forums.phpfreaks.com/topic/222638-classes/#findComment-1151383
Share on other sites

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.