I've got a class 'Event' which I am creating objects from via mysqli_fetch_object. The __construct() function is running and the objects variables are being set but they aren't set within the __construct() function.
I am using the following line to create the object:
$events[$x] = $result->fetch_object("Event")
When I run the following function by calling $events[$x]->eventPlaces(); it echos the variable.
public function eventPlaces()
{
echo $this->capacity;
}
However with the same code in the construct function it echos nothing.
public function __construct()
{
echo $capacity;
echo $this->capacity;
}
Apologies if I have explained this poorly, I've just got back into coding and OO php is new to me, if I missed anything then let me know.
I think you want your class to look like this:
class Event{
protected $capacity;
public __construct($capacity){
$this->capacity = $capacity;
}
public function eventPlaces(){
return $this->capacity;
}
}
Then you would do this:
$events[$x] = $result->fetch_object("Event", array(12));
echo $events[$x]->eventPlaces();
Related
I have the following:
config.php
class myObject {
public $_access_token;
public function __construct() {
$this->_access_token = '';
}
}
I need to pull the _access_token. Is this the best way to do it?
index.php:
require("config.php");
class gotime
{
public function getAccessToken(){
$obj = new myObject();
return $obj->_access_token;
}
Can I create the class outside the public function? It seems so inefficient to be putting a class creation in every function. I have another 12 variables I need to pull across files and I would like to set them in one place.
You can also use them like this.
class myObject
{
public static $_access_token;
public function __construct()
{
self::$_access_token = 'some value';//set the value you want
}
}
echo myObject::$_access_token;
I'm new to object oriented php. And if there are no functions in the method testing() in the HumanClass, should i declare them as abstract?
<?php
class HumanClass
{
private $legs;
private $hands;
public function __construct($legs, $hands)
{
$this->legs = $legs;
$this->hands = $hands;
}
public function testing()
{
}
}
class StudentClass extends HumanClass
{
private $books;
public function __construct($legs, $hands, $books)
{
parent::__construct($legs, $hands);
$this->books = $books;
}
public function testing()
{
echo "StudentClass called.";
}
}
function callClass(HumanClass $c)
{
$c->testing();
}
$example = new StudentClass(4, 2, 1);
callClass($a);
?>
Is it possible to have something like this?
echo $a->testing();
instead of having another method to call testing().
Given the code that you give, it's far from clear what the testing() function is supposed to do other than just exist for you to try things. The answer to that will also determine whether the versions in the baseclass should remain there as empty function.
There are other options, too, e.g. that the derived class first invokes the baseclass (extending), or that the baseclass doesn't contain an abstract or concrete such function but only the derived one does. Which to choose is up to the informed programmer to decide.
Simple question, is it possible to access a static variable from a $this-> call?
class testA
{
public static $var1 = "random string";
// current solution
public function getVar()
{
return self::$var1;
}
}
class testB
{
private $myObject;
public function __construct() {
$this->myObject = new testA();
// This line is the question
echo $this->myObject::var1;
// current solution
echo $this->myObject->getVar();
}
}
I'm afraid I've answered my own question. But having a few static variables I didn't want to have a function for each variable, Or even a single getVar($staticVar) when I could access it directly.
If this is the only solution. Any recommendations on a better way to implement this.
If I'm going to require a function call for each, I might as well get rid of the static variables altogether.
//method
public function staticVar1() {
return (string) 'random string';
}
You simply access the variable like this:
testA::$var1;
So using your exemple, it would be
class testB
{
private $myObject;
public function __construct() {
$this->myObject = new testA();
// This line is the question
echo testA::$var1;
// current solution
echo $this->myObject->getVar();
}
}
Try to understand the purpose of static.
static makes them accessible without needing an instantiation of the class.
They should accessed as below if the static variable is in the class
self::$var1;
below is possible in your case
testA::$var1;
would do the job here.
I have an abstract page class looking like this:
abstract class Page {
public static function display() {
self::displayHeader();
self::displayContent();
self::displayFooter();
}
public static function displayContent() {
print "<p>some content</p>";
}
public static function displayHeader() {
include_once(kContent . "HeaderContent.class.php");
HeaderContent::display();
}
public static function displayFooter() {
include_once(kContent . "FooterContent.class.php");
FooterContent::display();
}
};
I would like to subclass from this, and only override the displayContent method, so the header and footer is being displayed automatically, but still having the option to override the display method, for example for .js files.
Now I have another class, looking like this:
class FooPage extends Page {
public static function displayContent() {
print "<p>Foo page</p>";
};
Now, instead of calling the FooPage's displayContent method, it just calls the one from the superclass.
Why? What can I do?
EDIT
I'm running PHP 5.2.17
Ilija, PHP < 5.3 doesn't have "Late Static Binding" and that's why you may be experiencing the FooPage::displayContent not being called. If you are running PHP 5.2 then there is nothing much to do (except for some hacks using debug_backtrace(), which honestly I wouldn't recommend for this situation).
Now, what it really calls my attention is that your methods are all static; is there a reason for this? Why aren't they instance methods? I would expect something like:
include_once(kContent . "HeaderContent.class.php");
include_once(kContent . "HeaderContent.class.php");
abstract class Page
{
protected $header;
protected $footer;
public function __construct()
{
$this->header = new HeaderContent();
$this->footer = new FooterContent();
}
public function display()
{
$this->displayHeader();
$this->displayContent();
$this->displayFooter();
}
public function displayContent()
{
print "<p>some content</p>";
}
public function displayHeader()
{
$this->header->display();
}
public function displayFooter()
{
$this->footer->display();
}
};
class FooPage extends Page
{
public function displayContent()
{
print "<p>Foo page</p>";
}
}
and later in your view you would write something like:
$page = new FooPage();
$page->display();
Some things to take into account:
It is generally better not to use print/echo when generating a view content. Instead try to create the string and do the print/echo as a last step. This makes it easier to later write tests.
Example:
public function display()
{
return
$this->displayHeader() .
$this->displayContent() .
$this->displayFooter();
}
public function displayContent()
{
return "<p>some content</p>";
}
public function displayHeader()
{
return $this->header->display();
}
....
$page = new FooPage();
echo $page->display();
If you need to do it as your application grows, you can pass the header and footer as Page constructor parameters. As long as they are objects that understand the display() message (i.e. polymorphic) things should be ok.
HTH
Returned back to this question. Was looking for solution for Symfony (5.4).
And I finally came with this "Service - method call" solution.
#services_dev.yaml:
Company\Core\PinGenerator\PinGenerator:
calls:
- [setDebugMode, [true]]
#PinGenerator:
class PinGenerator implements PinGeneratorInterface
{
public static bool $inDebugMode = false;
public static function setDebugMode(bool $inDebugMode): void
{
self::$inDebugMode = $inDebugMode;
}
public static function generate(int $length = self::DEFAULT_PIN_CODE_LENGTH, bool $numbersOnly = true): string
{
if (!self::$inDebugMode) {
return PinGeneratorProd::generate($length, $numbersOnly);
} else {
return PinGeneratorDev::generate($length, $numbersOnly);
}
}
}
Honesly hoping, that this will help someone, someday.
I'm new to OOP and I can't figure out why this isn't working. Is it not ok to instantiate a class with in a class. I've tried this with included file with in the method and it didn't make a change.
include('Activate.php');
class First {
function __construct() {
$this->activate();
}
private function activate() {
$go = new Activate('Approved');
}
}
$run = new First();
Are you saying that you want to access $go? because, if that is the case, you need to change the scope of it.
As you see, $go in this method is only available inside activate():
private function activate() {
$go = new Activate('Approved');
}
to make it reachable from other location within the class, you would need to declare it outside activate():
private $go = null;
you call $go by using $this:
private function activate() {
$this->go = new Activate('Approved');
}
After that, if you want to access go from outside class, you would need to create wrapper:
public function getGo(){
return $this->go;
}
Hope this helped. Also, you can read the documentation about OOP in PHP.
Okay I figured out my issue. My code had two errors. One that being my method was set to private and secondly I had an error in my $parms that I was sending to the class.
include('Activate.php');
class First {
function __construct() {
$this->activate();
}
private function activate() {
$go = new Activate('Approved');
}
}
$run = new First();
You can access your private method with the help of create another public function in the same class and call the private function in the public method or function. Now you can call your private function with the help of $run instance.
For example:
class Cars{
function getting(){
echo ("Hello World");
}
private function getting2(){
echo ("Hello World Againg");
}
public function getting3(){
$this->getting2();
}
}
$class_object=new Cars;
print($class_object->getting3());
Your output screen like the below image.