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
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
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
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
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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