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,179 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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:rgb192
Comment Utility
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 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
Comment Utility
okay.  I will have a related question

thanks
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …

743 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now