If you have an abstract class, is there a way to keep a counter inside the abstract class to count how many times it's used?
Because if I do this:
abstract class abstractClassName {
private $counter = 0;
public function __construct() {
$this->counter++;
}
public function outputCounter() {
echo $this->counter;
}
}
class someExtension extends abstractClassName {
public function __construct() {
parent::__construct();
}
}
class someExtensionTwo extends abstractClassName {
public function __construct() {
parent::__construct();
}
}
and then
$class = new someExtension;
$class->outputCounter();
$class2 = new someExtensionTwo;
$class2->outputCounter();
I get 1 twice, yet I was expecting to get 1 and then 2 on the last call, and I'm getting confused over how it's all meant to work.
Edit: changed code to reflect the real code more :)
If you are wishing to keep track of how many time the class AbstractClassName has been instantiated, you will need to make use of static variables -- so that the value of $this->counter is persistent, for example:
abstract class abstractClassName
{
private static $counter = 0;
public function __construct() {
self::$counter++;
}
public function outputCounter() {
echo self::$counter;
}
}
class someExtension extends abstractClassName
{
public function __construct() {
parent::__construct();
}
}
class someOtherExtension extends abstractClassName
{
public function __construct() {
parent::__construct();
}
}
$class = new someExtension;
$class->outputCounter();
$class2 = new someOtherExtension;
$class2->outputCounter();
Which would result in an output of: 1 & 2.
Related
I have the following code
<?php
class ingredient {
private $calories;
public function __construct() {
$this->calories = 100;
}
public function get_calories() {
return $this->calories;
}
}
class flour extends ingredient {
private $texture;
public function __construct(texture) {
$this->texture = $texture;
}
public function get_texture() {
return $this->texture;
}
}
$plain_flour = new flour("Grainy");
My question is, does plain_flour have the attribute calories initialized already? Can I call get_calories on plain_flour? If no, how can I make it work so flour has the calories attribute ready to go and I can use the get_calories function
Since the $calories element is part of the ingredient class and is a private variable.
To access the variable from the flour class, you can call the parent's __construct method.
See this:
class ingredient {
private $calories;
public function __construct() {
$this->calories = 100;
}
public function get_calories() {
return $this->calories;
}
}
class flour extends ingredient {
private $texture;
public function __construct($texture) {
$this->texture = $texture;
parent::__construct();
}
public function get_texture() {
return $this->texture;
}
}
$plain_flour = new flour("Grainy");
echo $plain_flour->get_calories();
I want to use variable value from one function into another function of same class. I am using abstract class using which I am declaring variable as global indirectly. I can not declare variable as global in the class. My demo code is as follows:
<?php
abstract class abc
{
protected $te;
}
class test extends abc
{
public function team()
{
$te = 5;
$this->te += 100;
}
public function tee()
{
$tee = 51;
return $this->te;
}
}
$obj = new test();
echo $obj->tee();
//echo test::tee();
?>
Is this possible that I can echo 105 as answer there?
My main motive is I want to learn that how to get variable value from one function into another using without declaring that global in the same class Please let me know Is this possible OR I need to delete my question ?
<?php
abstract class abc
{
protected $te;
}
class test extends abc
{
public function __construct() {
$this->te = 5;
}
public function team()
{
$this->te += 100;
}
public function tee()
{
return $this->te;
}
}
$obj = new test();
$obj->team();
echo $obj->tee();
-- edit: to make at least some use of the abstract "feature":
<?php
abstract class abc
{
protected $te;
abstract public function team();
public function tee()
{
return $this->te;
}
}
class test extends abc
{
public function __construct() {
$this->te = 5;
}
public function team()
{
$this->te += 100;
}
}
$obj = new test();
$obj->team();
echo $obj->tee();
-- edi2: since you've asked whether you must invoke team (and then deleted that comment):
<?php
abstract class abc
{
protected $te;
abstract public function team();
public function tee()
{
$this->team();
return $this->te;
}
}
class test extends abc
{
public function __construct() {
$this->te = 5;
}
public function team()
{
$this->te += 100;
}
}
$obj = new test();
echo $obj->tee();
So, yes, it has to be invoked somewhere. But depending on what you're trying to achieve there are numerous ways to do so.
Each property of the class can be accessed by each method of the same class. So you can create methods which are working with the same property. And you don't need to create parent abstract class.
class test
{
protected $te = 5;
public function team()
{
$this->te += 100;
}
public function tee()
{
return $this->te;
}
}
$obj = new test();
$obj->team();
echo $obj->tee();
interface A
{
public function method1();
public function method2();
}
abstract class B implements A
{
public $publicc = 2;
public function method1()
{
echo "in method1 of B<br>";
}
}
class C extends B
{
public $publicc = 4;
public function __construct()
{
}
public function method2()
{
}
public function method1()
{
echo $this->publicc + parent::$publicc; // error for using parent::$publicc
}
}
$obj = new C();
$obj->method1();
But php throws error echo $this->publicc + parent::$publicc. I just want to get parent class $publicc property directly that has value 2, Without using any accessor method. Is there a way to do this in php?
It depends on what publicc exactly holds but a constant might suit your needs?
interface A
{
public function method1();
public function method2();
}
abstract class B implements A
{
const PUBLICC = 2;
public function method1()
{
echo "in method1 of B<br>";
}
}
class C extends B
{
const PUBLICC = 4;
public function __construct()
{
}
public function method2()
{
}
public function method1()
{
echo self::PUBLICC + parent::PUBLICC; // error for using parent::PUBLICC
}
}
$obj = new C();
$obj->method1();
I think you want a static property. If it is a property you want to access without an instance, that usually indicates a candidate for a static variable.
abstract class B implements A
{
protected static $publicc = 2;
...
}
class C extends B
{
public $publicc = 4;
public function __construct()
{
}
public function method2()
{
}
public function method1()
{
echo $this->publicc + parent::$publicc; // error for using parent::$publicc
}
}
I have this code and i´m trying to use a object
<?php
class Controller {
public $_view;
public function __construct() {
$this->_view = new View();
return $this->_view;
}
}
class View {
public $_params = array ();
public function set_params($index_name,$valores) {
$this->_params[$index_name] = $valores;
}
public function get_param($index_name){
return $this->_params[$index_name];
}
}
?>
i would like to do this:
class Index extends Controller {
public function index() {
$model = Model::get_estancia();
$usuarios = $model->query("SELECT * FROM usuarios");
$this->_view->set_params(); // cant be used.
$this->load_view("index/index");
}
}
i would like to use the set_parms function.
but i can't see the View Function, then i can not use.
Can someone explain and advise me a good and safe way?
Correction from Phil: If a __construct() method isn't found, PHP will revert to legacy constructor syntax and check for a method with the same name as the object. In your case the method index() is being treated as the constructor, and is preventing the parent's constructor from loading the view object into the $_view property.
You can force a class to inherit a parent's constructor by defining __construct() in the child and calling the parent's constructor:
public function __construct() {
parent::_construct();
}
Here is the fixed code:
<?php
class Controller {
public $_view;
public function __construct() {
$this->_view = new View();
return $this->_view;
}
}
.
class View {
public $_params = array ();
public function set_params($index_name,$valores) {
$this->_params[$index_name] = $valores;
}
public function get_param($index_name){
return $this->_params[$index_name];
}
}
.
class Index extends Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$model = Model::get_estancia();
$usuarios = $model->query("SELECT * FROM usuarios");
$this->_view->set_params(); // cant be used.
$this->load_view("index/index");
}
}
for example -
class wow
{
public $foo = 5;
public function __construct()
{
$sub_class = new sub();
}
}
class sub
{
public function __construct()
{
echo $this->foo;
}
}
$wow = new wow();
Is there a way of doing this?
why the f*** always people minusing my questions? What is your problem? This site is for asking questions, if you have a problem so don't come to this site.
Adding on from my comment.. try this
class wow
{
public $foo = 5;
public function __construct()
{
$sub_class = new sub();
}
}
class sub extends wow
{
public function __construct()
{
echo $this->foo;
}
}
$wow = new wow();
You should extend the parent class
class wow
{
public $foo = 5;
public function __construct()
{
$sub_class = new sub();
}
}
class sub extends wow
{
public function __construct()
{
echo $this->foo;
}
}
$wow = new wow();
This will return 5.
And you have other errors in your code
class sub()
Should be
class sub
And
public __construct()
Should be
public function __construct()