Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-11-27
6
Medium Priority
?
1,232 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 111

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 111

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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

Hire Technology Freelancers with Gigs

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

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

705 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