PHP Method, Objects - php

I have the following PHP code (below) but cannot seem to get it to run? I think I'm missing something simple like a function (?) or other type of code(s)? It should print: My Antonia: Willa Cather (5.99).
Error message:
Fatal error: Call to undefined method shopProduct::getProducer() in C:\Program Files (x86)
<?php
class shopProduct
{
//class body
}
class ShopProductWriter
{
public function write($shopProduct)
{
$str = "{$shopProduct ->title}: " . $shopProduct->getProducer() . " ({$shopProduct -> price})\n";
print $str;
}
}
// we can test it like this
$product1 = new ShopProduct("My Antonia", "Willa", "Cather", 5.99);
$writer = new ShopProductWriter();
$writer->write($product1);
print "Author: {$product1->getProducer()}\n";
?>

Related

How I can create function prev_id?

How I can create function prev_id for example code using mysqli_fetch_array:
$idprev = prev_id();
$sql = "SELECT * FROM site WHERE id='$idprev' ";
This function not exist in php.
Fatal error: Uncaught Error: Call to undefined function prev_id()
you need to define function prev_id() first before using it.
<?php
function prev_id()
{
// your code lines
return $yourvariable;
}
?>

Unknown error when running command line Phalcon app

I'm trying to run a command-line task, and my cli.php file is giving me this error:
PHP Notice: Array to string conversion in /var/www/htdocs/classschedule/app/cli.php on line 23
PHP Fatal error: Uncaught RuntimeException: Call to undefined method ::gettaskname() in /var/www/htdocs/classschedule/app/cli.php:23
Stack trace:
#0 /var/www/htdocs/classschedule/app/cli.php(23): Phalcon\Cli\Console->handle(Array)
#1 {main}
thrown in /var/www/htdocs/classschedule/app/cli.php on line 23
Here is my cli.php
include '/var/www/common/dump.php';
require 'config/bootstrap.php';
$DI->get('dispatcher')->setDefaultNamespace('Task');
$DI->get('dispatcher')->setNamespaceName('Task');
$Console = new \Phalcon\CLI\Console();
$Console->setDI($DI);
$arguments = [];
foreach($argv as $k => $arg) {
if($k == 1) {
$arguments['task'] = $arg;
} elseif($k == 2) {
$arguments['action'] = $arg;
} elseif($k >= 3) {
$arguments['params'][] = $arg;
}
}
try{
$Console->handle($arguments); // <-- This is line 23
}
catch(\Phalcon\Exception $e){
echo $e->getMessage();
exit(255);
}
I have no idea why either the Notice or Fatal error are getting generated. This file is almost identical to the cli.php for another app I have, that runs just fine. Even taking out the foreach() still causes the error.
Edit:
Bootstrap.php
Config.php
Solved
Solution:
My DI, Dispatcher, and Router were all MVC versions instead of their CLI equivalents. Changing them fixed the problem - setTask() was expected in the Dispatcher.
Could you please share your config/bootstrap.php file? I tested with:
use Phalcon\Di\FactoryDefault\Cli as DI;
Parameters were read and line 23 was asking for MainTask handler class (no error).
This is the code I tested:
use Phalcon\Loader;
use Phalcon\Di\FactoryDefault\Cli as CliDI;
$DI = new CliDI();
$loader = new Loader();
$loader->registerNamespaces(
[
'Task' => __DIR__ . '/tasks',
]
);
$loader->register();
$Console = new \Phalcon\CLI\Console();
$Console->setDI($DI);
$arguments = [];
foreach($argv as $k => $arg) {
if($k == 1) {
$arguments['task'] = $arg;
} elseif($k == 2) {
$arguments['action'] = $arg;
} elseif($k >= 3) {
$arguments['params'][] = $arg;
}
}
try{
$Console->handle($arguments);
}
catch(\Phalcon\Exception $e){
echo $e->getMessage();
exit(255);
}
And MainTask.php:
namespace Task;
use Phalcon\Cli\Task;
class MainTask extends Task
{
public function mainAction()
{
echo 'This is the default task and the default action' . PHP_EOL;
}
public function testAction(array $params)
{
echo sprintf('hello %s', $params[0]);
echo PHP_EOL;
echo sprintf('best regards, %s', $params[1]);
echo PHP_EOL;
}
}
$Console->handle($arguments); // <-- This is line 23
It seems that this line is expecting a string and you're passing an array.
Maybe phalcon is not handling this case well and can't instantiate some other object on which it tries to call gettaskname on.

Class not found although it's in an included file

In my main page (index.php) I've included "class_lib.php" which contains the class definitions.
When calling the class from index.php - I'm getting the followin
Fatal error: Uncaught Error: Class 'person' not found in /home/latingate/public_html/test/ObejectOriented/index.php:5 Stack trace: #0 {main} thrown in /home/latingate/public_html/test/ObejectOriented/index.php on line 5
What am I doing wrong?
index.php
<?php include("class_lib.php"); ?>
<?php
$stefan = new person();
$jimmy = new person;
$stefan->set_name("Stefan Mischook");
$jimmy->set_name("Nick Waddles");
echo "Stefan's full name: " . $stefan->get_name();
echo "Nick's full name: " . $jimmy->get_name();
?>
class_lib.php
<?php
class person {
var $name;
function set_name($new_name) {
$this->name = $new_name;
}
function get_name() {
return $this->name;
}
}
?>
you can see it online here:
view online
Your code is correct and works for me.
I think Problems in file Path. So, first check your class_lib.php file is not in same directory then assign correct path.

php mvc router not working

i have php mvc project .
This is my router class.
router class job is include controller in main index page of website.
its work on localhost but when i uploaded on my host not working
what i do ?
<?php
class route
{
function __construct()
{
if(empty($_GET['url']))
{
$url = "index";
}
else
{
$url = $_GET['url'];
}
$url = explode("/",$url);
if(!empty($url[0]))
{
$controllername = "controllers/".$url[0].".php";
if(file_exists($controllername))
{
require($controllername);
$object = new $url[0];
//$object->loadmodel($url[0]);
if(!empty($url[1]))
{
if(method_exists($object,$url[1]))
{
if(!empty($url[2]))
{
$object->$url[1]($url[2]);
}
else
{
$object->$url[1]();
}
}
else
{
echo "Error";
}
}
else
{
echo "Error";
}
}
else
{
echo "Error";
}
}
}
}
this is my error in error_log
thrown in /home/mogeir/public_html/libs/route.php PHP Notice: Array
to string conversion in /home/mogeir/public_html/libs/route.php on
line 33 PHP Notice: Undefined property: admin::$Array in
/home/mogeir/public_html/libs/route.php on line 33 PHP Fatal error:
Uncaught Error: Function name must be a string in
/home/mogeir/public_html/libs/route.php:33
Use the following function call forms instead - the commented ones are correct alternatives too. See call_user_func and call_user_func_array.
if (!empty($url[2])) {
$object->{$url[1]}($url[2]);
// Alternative 1
// call_user_func(array($object, $url[1]), $url[2]);
// Alternative 2
// call_user_func_array(array($object, $url[1]), array($url[2]));
} else {
$object->{$url[1]}();
// Alternative 1
// call_user_func(array($object, $url[1]));
// Alternative 2
// call_user_func_array(array($object, $url[1]), array());
}
You said:
its work on localhost but when i uploaded on my host not working what i do ?
That has to do with the error reporting activation or with difference in PHP version.

How to use Request in Class PHP

I'm try to call request from web but not working , this code call command game server from website.
class SampRconAPI
{
private $command;
public function __construct()
{
$this->command = $_REQUEST["command"];
}
if($this->command == "cmdlist")
{
$aCommands = $this->packetSend('cmdlist');
unset($aCommands[0]);
foreach($aCommands as &$sCommand)
{
$sCommand = trim($sCommand);
}
return $aCommands;
}
My Error:
A PHP Error was encountered
Severity: Notice
Message: Undefined index: command
Filename: include/SampRcon.php
Line Number: 7
You can rewrite below line:
$this->command = $_REQUEST["command"];
something like this:
$this->command = isset($_REQUEST["command"]) ? $_REQUEST["command"] : "";
So that it won't give you error.
your error made couldn't found command pram on GET request . first check you service configuration is ok .
<?php
$rcon = new SampRconAPI('server_ip', server_port, 'server_rcon_pass');
$rcon->Call('name ' . $playerid_here);
print_r( $rcon->getInfo());
http://forum.sa-mp.com/showthread.php?t=104299
here is example https://gist.github.com/Westie/234209
?>

Categories