call class $this from external function [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
inside a template php files, i have hundreds of such code:
echo str_replace('x','y', $this->load1->view('something'));
i have replaced that line everywhere, with code:
echo blabla();
and placed a function in my core library:
function blabla(){
return str_replace('x','y', $GLOBALS['this']->load1->view('something'));
}
but it triggers error: Fatal error...

In PHP, $this refers to the current object. For Example:
class MyClass {
protected $attribute;
public function method() {
$this->attribute;
}
public static function staticMethod() {
//$this is not available here because of the static context!
}
}
$this is used inside the MyClass.
For more details: http://php.net/manual/en/language.oop5.basic.php

Re: your edited question, $this only exists within the class.
You have two options:
function blabla($something){
str_replace('x','y', $something);
}
blablah($this->load1->view('something'));
Or, put function blabla() { inside the class, and drop the global $this line.

Related

Is it bad practice to call a static method via an object? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I found that calling the static method via object can be very convenient in some use case.
I'm wondering if this is is considered as a bad practice?
or if this feature will be removed in the future version of PHP?
class Foo
{
public static function bar ()
{
echo 'hi';
}
}
class SubFoo extends Foo
{
public static function bar ()
{
echo 'hi subfoo';
}
}
// The normal way to call a static method.
Foo::bar(); // => "hi"
// Call the static method via instance.
$foo = new Foo;
$foo::bar(); // => "hi"
// Here is the use case I found calling static method via instance is convenient.
function callbar(Foo $foo)
{
// The type-hinting `Foo` can be any subclass of `Foo`
// so I have to figure out the class name of `$foo` by calling `get_class`.
$className = get_class($foo);
$className::bar();
// Instead of the above, I can just do `$foo::bar();`
}
callbar(new SubFoo); // => "hi subfoo"
As a general rule, using static methods is bad practice because:
In fact, static methods or static variables are global variables
static code makes can cause many troubles in testing
static code makes high cohesion between parts of code
static code makes hidden dependencies between parts of code
But, there are cases when using static code is justified. For example:
Methods refer to a class and don't refer to objects
Helpers or Util classes which don't have their states

Error while Accessing Private Func. from Public Func. inside a Class [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I have the following class structure. When run the post() function, I receive an error Uncaught Error: Call to undefined function curl_post().
class POST {
private function encode($data) {
}
private function curl_post($data) {
encode($data);
}
public function post($data) {
$post = curl_post($data);
print_r($post);
}
}
What is causing this error? Do I have to use something like $this-> to access the private function?
To call sibling object methods, prefix the call with $this->:
class POST {
private function encode($data) {
}
private function curl_post($data) {
$this->encode($data);
}
public function post($data) {
$post = $this->curl_post($data);
print_r($post);
}
}
From the documentation:
The pseudo-variable $this is available when a method is called from within an object context. $this is a reference to the calling object (usually the object to which the method belongs...

How can I get a public variable in PHPOO? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm trying to put a 'global' value in a PHP controller, because I use it constantly in different functions, so, I've to send it a lot of times in the view, I saw that I can do a public variable like an attribute in Java.
I have to declare the variable:
var $team="";// or public $team="";
How can I put, and get the value of the PHP attribute?
Class Example:
class MyClass
{
private $_a;
public $b;
public function __construct()
{
// example of constructor
}
public function set($a)
{
$this->_a = $a;
}
public function get()
{
return $this->_a;
}
}
Example of usage:
// usage construct class and invoke private function set();
$myExampleClass = new MyClass();
$myExampleClass->set('something');
// set public
$myExampleClass->b = "i set a public var";
It probably best to design your class with getters and setters so keeping your variables private, unless you have specific reasons. Here is a good reference for best practices and styleguide.
I hope this helps.

How php requires a class inside a class function? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a class:
class Test {
public function someFunction()
{
require('SomeOtherClass.php');
}
}
I can't understand how php requires the class here if another class physically can't be inside a function of a class? how php does this? where php puts the class then?
This is in the documentation for include:
When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope. (emphasis added)
require is identical to include in virtually all respects, including this one.
Use the following in the class:
class Test {
public $other_class;
function __construct() {
$this->other_class = new other_class();
}
public function someFunction() {
$this->other_class;
}
}
To use this. Inlcude your classes like this:
spl_autoload_register(function($class) {
include_once 'classes/'.$class.'.php';
});
Use that function in a file that is included everywhere
This is perfectly valid code:
function foo() {
class Foobar {
function speak() {
echo "Hi";
}
}
$obj = new Foobar();
$obj->speak();
}
// $obj = new Foobar(); // Will fail, as Foobar will be defined the global scope when running foo();
foo();
$obj = new Foobar(); // Will be OK, now the class is defined in the global scope
//foo(); // Fatal error: Cannot redeclare class Foobar
The output is:
Hi
For details see documentation.

Object oriented php object,class and methods [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
$Clint_ip=$this->request->clintIp();
May I get a clear concept about this line?In here I know $clint_ip is a variable,but what is the next three?which one is an object?
which one is a method?
which one is a class?
I just need to understand this line.In several project I have seen this types of line.In this line which one called object?If you want You can give another example.In here $this is an object?or class?or method?
Yes $Clint_ip is an variable,
Like other object oriented based programming languages $this is the this of a class consisting it. (For more about this When to use self over $this?)
request looks like an object of another class
and clintIp() is the public method of the class of the request object
The code you provided appears to be from inside of a class.
A class is denoted like this:
class Example {
private $foo;
public $bar;
public function __construct() {
}
public function method() {
}
private function other() {
}
}
When you create an object of this class, you can use the format:
$example = new Example();
This calls the constructor __construct().
Once you have created ("instantiated") this object, you can use the -> to call the properties of the object.
So, I can say
$example->bar = "Foo";
which sets this property to a string.
Your Code
In your code, the property "request" is itself an object (an instance of a class).
$Clint_ip=$this->request->clintIp();
Here is an example of the code this could be using
class Example {
public $request;
public function __construct($request) {
$this->request = $request;
}
}
class Request {
public function clintIp() {
//return something
}
}
And then some context:
$request = new Request;
$example = new Example($request);
$clint_ip = $example->request->clintIp();
So here, $clint_ip is the variable. $example and $request are objects (instances of classes), and clintIp() is a method of the request object.
Now, about "$this". This indicates that it is within the object "Example":
Imagine the class Example now has a method
public function test() {
return $this->request->clintIp();
}
$this means that it is inside of an instance of an object. In static context, use "self::", as mentioned in one of the other answers.
You are inside object which has request property. Request property contains object with method clintIp() which return client ip.

Categories