0

I am a beginner in php. I want to call a class within the same php file but when i am trying the class itself is not recognized. What i am doing wrong. please guide me through the right direction since i just started writing code in php.

Code:

<?php
$details = new studdetails();
$details->id = $sid;
$details->name = $sname;
$details->roll = $sroll;

Somemethod("Have some calculations over here");

Class studdetails{ 
   public id; 
   public name; 
   public roll;
}
?>
Sudha
  • 375
  • 1
  • 5
  • 16
  • There is a ; missing after `$details->roll = $sroll;` – codedge May 12 '16 at 21:22
  • Possible duplicate of [PHP Parse/Syntax Errors; and How to solve them?](http://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them) – Don't Panic May 12 '16 at 21:25
  • @Don'tPanic I missed the semicolon while copying. so maybe some other issue. – Sudha May 12 '16 at 21:28
  • After your edit, `Somemethod` is no longer a syntax error, now it is just an undefined function. And all of your class variables are still missing `$` before their names. – Don't Panic May 12 '16 at 21:30
  • When declaring class properties you need to use the `$`. e.g. `public $id;`. However, do not include the `$` when accessing them `$detail->id` (you already had that right) – Dan May 12 '16 at 21:31
  • `Somemethod` is not defined. did you mean to define a method for your class? – Dan May 12 '16 at 21:32
  • Always `error_reporting(E_ALL); ini_set('display_errors', '1');` – AbraCadaver May 12 '16 at 21:36

2 Answers2

0

There are some syntax errors. Missing ";" and "$".

<?php

Class studdetails { 
    public $id; 
    public $name; 
    public $roll;
}

$details = new studdetails();
$details->id = $sid;
$details->name = $sname;
$details->roll = $sroll;

?>
Jonah
  • 71
  • 8
0

In php you need to prefix class properties with $ sign.

See the example bellow

$sid = 1;
$sname = 'name';
$sroll = 'sroll';

$details = new studdetails();
$details->id = $sid;
$details->name = $sname;
$details->roll = $sroll;

Somemethod("Have some calculations over here", $details);

Class studdetails
{
    public $id;
    public $name;
    public $roll;
}

function Somemethod($str, $obj) {
    echo $str . PHP_EOL;
    var_export($obj);
}
Richard
  • 1,045
  • 7
  • 11