output constructor variable value in oop php - php

My concept is very poor in oop for php. My class has a constructor with three parameters.i create a object and pass three values to the constructor. Now, how will i show constructor value.

class Foo {
public function __constructor($para1, $para2, $para3 ){
echo $para1 . '<br>';
echo $para2 . '<br>';
echo $para3 . '<br>';
}
}
$f = Foo(10,20,30);

Related

Passing variables to obejct method on Flight PHP

According to the Flight PHP documentation, to use an object method is by using:
Flight::route('/some/route', [$object, 'method']);
and to use route parameters is by using:
Flight::route('/#name/#id', function($name, $id){
echo "hello, $name ($id)!";
});
I tried to combine both like this:
Flight::route('/user/#id', [$object, 'method']);
but it doesn't work. Is there any way to pass parameters to an object method?
How about assigning the variables in the closure?
Flight::route('/#name/#id', function($name, $id){
$obj = new Object; // or use a DIC
$obj->name = $name;
$obj->id = $id; // or assign these in the constructor
});
Looking at Dispatcher.php (methods callFunction and invokeMethod), your use case is supposed to be supported. Parameters should be supported equally well in anonymous functions and in class methods...
This code works for me:
function someFunction($id) {
echo 'id: ' . $id;
}
class SomeClass {
function method1($id) {
echo 'Class, id: ' . $id;
}
function method2($name, $id) {
echo 'Class, name: ' . $name . ', id: ' . $id;
}
}
$object = new SomeClass();
Flight::route('/user/#id', array($object, 'method1'));
Flight::route('/user/#id/#name', array($object, 'method2'));
Flight::route('/fun/#id', 'someFunction');
I am not good wit PHP but it's something with callbacks:
https://www.exakat.io/the-art-of-php-callback/

how to select this class in php?

php:
class MyClass
{
const CONSTANT = 'constant value';
function showConstant() {
echo self::CONSTANT . "\n";
}
}
echo MyClass::CONSTANT . "\n"; //this way selects MyClass.
My question is that could I select this class as following:
echo $this->CONSTANT . "\n"; // I think it's not right way, how could I do?
I want to select current class.
I'm very new to php and while learning php this question came into my mind. So forgive me If I'm asking nonsense question.
CONST properties are accessible without the need of instantiating the class.
CONST are also shared across all the instances of the class in memory.
To access a CONST:
ClassName::CONST
or given a class referenced in a variable:
$myclassvar::CONST
Or in class Context:
self::CONST
This is well explained here:
http://www.php.net/manual/en/language.oop5.constants.php
<?php
class MyClass
{
const CONSTANT = 'constant value';
function showConstant() {
echo self::CONSTANT . "\n";
}
}
echo MyClass::CONSTANT . "\n";
$classname = "MyClass";
echo $classname::CONSTANT . "\n"; // As of PHP 5.3.0
$class = new MyClass();
$class->showConstant();
echo $class::CONSTANT."\n"; // As of PHP 5.3.0
?>
You can do this:
<?php
$a = new MyClass();
echo $a::CONSTANT;
?>

PHP5 OOP: Accessing changed parent properties

This is my first question, and something which has got me stumped. I'm not sure if this is something simple and I'm overlooking it or something not possible.
Below is a very simplified version of my original code. The end goal is to have the output as follows:
1:
2: this is a test
3: this is another test
4: this is another test
However, with the code in its current state, its actual output is this:
1:
2: this is a test
3: this is another test
4:
I want object 'B' to be able to access the value of test_variable AFTER first_function() has altered it.
It works fine when I declare test_variable as static, however in the actual application it wouldn't work and when I try to echo parent::test_variable it outputs 'Object ID #17' and so on.
class A
{
public $test_variable;
function __construct()
{
echo '1: ' . $this->test_variable . "<br />";
$this->test_variable = 'this is a test';
echo '2: ' . $this->test_variable . "<br />";
}
function first_function()
{
$this->test_variable = 'This is another test';
echo '3: ' . $this->test_variable . "<br />";
$b = new b;
$b->second_function();
}
}
class B extends A
{
function __construct()
{
/* Dont call parent construct */
}
function second_function()
{
echo '4: ' . $this->test_variable;
}
}
$a = new A;
$a->first_function();
// Outputs:
// 1:
// 2: this is a test
// 3: this is another test
// 4:
// but I want it to output
// 1:
// 2: this is a test
// 3: this is another test
// 4: this is another test
Many thanks for any responses. I greatly appreciate them.
Phil
Declaring public $test_variable; inside the class means each instance (object) of the class has a copy. $test_variable in class A does not point to the same memory address as $test_variable in Class B. This is done intentionally to allow scope and remove a global state. As you said before, declaring it static will work, because then each instance shares the same variable.
In this instance, $test_variable is, in essence, a dependency that class B requires. You can get that dependency via constructor injection fairly easily:
class A
{
public $test_variable;
function __construct()
{
echo '1: ' . $this->test_variable . "<br />";
$this->test_variable = 'this is a test';
echo '2: ' . $this->test_variable . "<br />";
}
function first_function()
{
$this->test_variable = 'This is another test';
echo '3: ' . $this->test_variable . "<br />";
// Instantiate instance passing dependency
$b = new b($this->test_variable);
$b->second_function();
}
}
class B extends A
{
function __construct($dependency)
{
// Set dependency
$this->test_variable = $dependency;
}
function second_function()
{
echo '4: ' . $this->test_variable;
}
}
$a = new A;
$a->first_function();
So, that's just a thought on how you might consider handling this.

get_class_vars() not showing a variable, but property_exists() run on the same class returns true

I'm studying PHP, and I've started playing with classes--below is possibly the most basic object ever, lol.
<?php
class Person {
var $first_name;
var $last_name;
var $arm_count = 2;
var $leg_count = 2;
function say_hello() {
echo "Hello from inside the class " . get_class($this) .".<br />";
}
function full_name() {
return $this->first_name . " " . $this->last_name;
}
}
$person = new Person();
echo $person->arm_count . "<br />";
$person->first_name = 'Lucy';
$person->last_name = 'Ricardo';
echo $person->full_name() . "<br />";
$vars = get_class_vars('Person');
foreach($vars as $var => $value) {
echo "{$var}: {$value}<br />";
}
echo property_exists("person","first_name") ? 'true' : 'false';
?>
Then the above runs, it is supposed to output a bit of data. In the lesson (a video training series by Kevin Skoglund, "PHP: Beyond the Basics",) Kevin's screen looks correct (he's using 5.2.6.)
I'm on 5.3 on my WAMP installation, and my "first_name" attribute of the class Person is not being spit out by the loop... yet the echo property_exists("person","first_name") ? 'true' : 'false'; returns true.
Can anyone help me understand what's going wrong?
property_exists will return true if the property exists, no matter what the scope of the property and the caller are.
get_class_vars will return all properties accessible from the current scope, along with their static values, or default values (for properties that are not declared static). However, it will not return properties that are not declared in the class body, nor will it accept an object argument.
Note that property_exists will also return false if a property that is not declared in the class body (i.e.: object context) is queried using the class name.
Per example:
class Foo {
public $foo;
private $bar;
public function test() {
var_dump(get_class_vars(__CLASS__));
}
}
$obj = new Foo;
$obj->baz = 'hello';
property_exists($obj, 'bar'); // true
property_exists($obj, 'baz'); // true
property_exists(get_class($obj), 'baz'); // false
get_class_vars(get_class($obj)); // you get "foo" only
$obj->test(); // you get "foo" and "bar", not "baz"
get_class_vars() returns only public access variables, while property_exists() checks for public, protected and private ones.
http://php.net/manual/de/function.get-class-vars.php
vs.
http://php.net/manual/de/function.property-exists.php

PHP Object Question

Unfortunately I cannot provide any code examples, however I will try and create an example.
My question is about Objects and memory allocation in PHP.
If I have an object, lets say:
$object = new Class();
Then I do something like
$object2 = $object;
What is this actualy doing? I know there is a clone function, but thats not what I'm asking about, I'm concerned about whether this is creating another identical object, or if its just assigning a reference to $object.
I strongly understand this to mean that it just creates a reference, but in some case usages of mine, I find that I get another $object created, and I can't understand why.
If you use the magic method __invoke, you can call an object similar to a function, and it will call that magic method.
class Object{
function __invoke(){ return "hi"; }
}
$object = new Object;
$object2 = $object();
echo $object2; // echos hi
That means that $object2 is equal to whatever that function returns.
Basically, you are calling a function, but using a variable as it's name. So:
function test(){ echo "hi"; }
$function_name = "test";
$function_name(); // echos hi.
In this case, you are just calling an object instead.
So, in reference to your question, this is actually not 'cloning' at all, unless the __invoke() function looks like this:
function __invoke(){ return this }
In which case, it would be a reference to the same class.
You are creating a second reference of the same object. Here is a proof:
<?php
class TestClass {
private $number;
function __construct($num) { $this->number = $num; }
function increment() { $this->number++; }
function __toString() { return (string) $this->number; }
}
$original = new TestClass(10);
echo "Testing =\n";
echo "--------------------------------\n";
echo '$equal = $original;' . "\n";
$equal = $original;
echo '$equal = ' . $equal . ";\n";
echo '$original->increment();' . "\n";
$original->increment();
echo '$equal = ' . $equal . ";\n";
echo "\n";
echo "Testing clone\n";
echo "--------------------------------\n";
echo '$clone = clone $original;' . "\n";
$clone = clone $original;
echo '$clone = ' . $clone . ";\n";
echo '$original->increment();' . "\n";
$original->increment();
echo '$clone = ' . $clone . ";\n";
Use clone if you want to create a copy of an instance.
Assuming that you mean
$object2 = $object;
And not
$object2 = $object();
PHP will create a reference to the original object, it will not copy it. See http://www.php.net/manual/en/language.oop5.basic.php, the section called
Object Assignment.
<?php
class Object{
public $value = 1;
public function inc(){
$this->value++;
}
}
$object = new Object;
$object2 = $object;
$object->inc();
echo $object2->value; // echos 2, proving it's by reference

Categories