Solved

Fatal error: Undefined class constant 'price' in C:\wamp\www\oop-hg\ts1.php on line 10

Posted on 2013-11-27
6
1,195 Views
Last Modified: 2013-12-01
class Books{
  //member variables
  var $price;
  var $title;
  //member functions
  function setPrice($par){
    $this->price=$par;
  }
  function getPrice(){
    echo $this->price.'<br>';
  }
  function setTitle($par){
    $this->title=$par;
  }
  function getTitle(){
    echo $this->title.'<br>';
  }
}

Open in new window


    echo $this->price.'<br>';

I want to do
    echo parent::$price.'<br>';
      echo self::$price.'<br>';

but I get error
Fatal error: Undefined class constant 'price' in C:\wamp\www\oop-hg\ts1.php on line 10
0
Comment
Question by:rgb192
  • 3
  • 3
6 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39680831
If you're working with a tutorial that suggests this, get rid of it and find one that is at least up to the level of PHP5!

var $price;

The var designator is about 10 years obsolete.  In the PHP5 object model we use public, protected or private to designate class properties.

Are you sure you have posted the SSCCE here?  I'd like to have something I can install and run to recreate the error.
0
 

Author Comment

by:rgb192
ID: 39681443
http://www.tutorialspoint.com/php/php_object_oriented.htm


I am adding code to tutorial because I want to learn by typing

echo $this->price.'<br>';

I want to do
    echo parent::$price.'<br>';
      echo self::$price.'<br>';
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39681649
I'm still not sure I understand what you want to do, but if it's just for study I recently got this book and I think it's very good.
http://www.amazon.com/PHP-Advanced-Object-Oriented-Programming-QuickPro/dp/0321832183/

If you want to use the :: scope resolution operator you must declare the $price property static.  That means you cannot instantiate an object from the class.  If you try it like this, it will fail.  If you want an object instance of the class, you must use $this->price to access the property.

See: http://www.laprbass.com/RAY_temp_rgb192.php

Outputs:
THIS PRICE: 3
Fatal error: Access to undeclared static property: Second::$price in /home/websitet/public_html/RAY_temp_rgb192.php on line 24

<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);

// http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28305101.html#a39681443

Class First
{
    protected $price;
    public function __construct($x)
    {
        $this->price = $x;
    }
}
Class Second Extends First
{
    protected $price;
    public function __construct($x)
    {
        $this->price = $x;
    }
    public function show_and_tell()
    {
        echo "THIS PRICE: $this->price";
        $abc = self::$price;
        echo "SELF PRICE: $abc";
        $abc = parent::$price;
        echo "PARENT PRICE: $abc";
    }
}

$obj = new Second(3);
$obj->show_and_tell();

Open in new window

0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:rgb192
ID: 39685460
Your code sample generates an error when I try static

echo $this->price.'<br>';

I want to do
    echo parent::$price.'<br>';
      echo self::$price.'<br>';

Open in new window


how to modify code so I can try all 3 of these options this,parent, self (for my own learning)
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39685489
Yes, of course it generates an error.  That's why I posted the output showing "fatal error."

If you want to do three things, you probably want to write three separate pieces of code.  Class properties are either static or not.  They aren't both.

If you want an object instance of the class, you use $this->

If you want a static property you use the scope resolution operator :: (Paamayim Nekudotayim)
0
 

Author Closing Comment

by:rgb192
ID: 39688817
okay.  I will have a related question

thanks
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question